当前位置:网站首页>@Principle of Autowired annotation
@Principle of Autowired annotation
2022-07-26 05:02:00 【blanceage】
One 、@Autowired
Concept :
@Autowired notes , It can be to Members of the class Variable 、 Method and constructor for annotation , Complete the work of automatic assembly . adopt @Autowired To eliminate set ,get Method .
In the use of @Autowired Before , We have a bean When configuring properties , It's using xml file , such as :
<property name=" Property name " value=" Property value "/>
But in use @Autowired after , We only need to use one where we need it @Autowired That's all right. . For example, we want to inject an attribute
public interface UserService {
public void getUser();
}
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserDao dao;
public void getUser() {
System.out.println(" obtain 2");
dao.getUser();
}
}
public interface UserController {
public void getUser();
}@Controller
public class UserControllerImpl implements UserController{
@Autowired
private UserService service;
public void getUser() {
System.out.println(" pick up information 3");
service.getUser();
}
}public interface UserDao {
public void getUser();
}@Repository
public class UserDaoImpl implements UserDao{
public void getUser() {
System.out.println(" Get user data ");
}
}test :
@Test
public void getUser(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
UserController user = applicationContext.getBean("UserController", USerController.class);
User.getUser();
}
We are using @Autowired Don't go again xml Continue to configure in the file .
Attention to detail :
1、 Use @Autowired The current class of must also be spring Container hosting ( hit @Coponent、@Controller、@Service 、@repository)
2、 Whether it's public and private Decorated fields can be automatically injected
3、 By default , Use @Autowired The attributes of annotations must be assembled , If this type of bean Inject , You're going to report a mistake . If it is allowed not to be assembled @Autowired Of required The attribute is false
4、@Autowired Is type based injection , If the current type attribute has only one in the container Bean, Then the attribute name is not limited , However, it is generally recommended to follow the rule of lowercase class names ‘
5、 If the current attribute type has more than one in the container Bean, Then you must pass the attribute name perhaps @Qualifier Appoint Bean name
6、@Autowired Can play in XXX[] 、List On , All... In the container will be XXX Type of bean Inject it all 、 And the attribute name has no constraint , But note that you can pass @Qualifier Specify injection specify beanName Of bean, Attribute names have no binding effect
7、@Autowired Can play in Map<String,XXX> On , All XXX Type of bean Will be injected ,beanName by key , The object is value, But note that you can pass @Qualifier Specify injection specify beanName Of bean, Attribute names have no binding effect
边栏推荐
- List转换为tree-项目真实使用
- C language lseek() function: move the read and write location of the file
- 常函数const的学习
- Bsdiff and bspatch incremental updates
- Database startup message: ora-29702: error occurred in cluster group service
- 遥感、GIS和GPS技术在水文、气象、灾害、生态、环境及卫生等领域中的应用
- [mathematical modeling] basic knowledge of MATLAB
- CountLaunch Demo的测试
- Kubernetes advanced training camp scheduler
- Study of const of constant function
猜你喜欢
![[weekly translation go] how to write your first program with go](/img/77/cf77a46340a39797382fd7b60517d5.png)
[weekly translation go] how to write your first program with go

Good at C (summer vacation daily question 6)

Learn to map with nature medicine -- complex heat map

Use field parameters for report translation

How to connect tdengine through idea database management tool?

STM32 development | ad7606 parallel multi-channel data acquisition

vector详解和迭代器失效问题

注解@Autowired如何自动装配

There was an unexpected error (type=method not allowed, status=405)

域名解析过程全分析,就着文字理解更佳
随机推荐
uniapp小程序框架-一套代码,多段覆盖
MySQL 执行失败的sql是否会计入慢查询?
minipcie接口CAN卡解决工控机扩展CAN通道的难题 minipcie CAN
异步时父子线程间的ThreadLocal传递问题
C语言——字符串函数,内存函数集锦以及模拟实现
AXI协议(4):AXI通道上的信号
The landing of tdengine in the GPS and AIS scheduling of Zhongtian steel
Two ways to create MySQL database
遥感、GIS和GPS技术在水文、气象、灾害、生态、环境及卫生等领域中的应用
Distance between bus stops: simple simulation problem
9 best project set management tools
Working principle and application of fast recovery diode
What are the restrictions on opening futures accounts? Where is the safest place to open an account?
【语义分割】2018-DeeplabV3+ ECCV
JVM第六讲:线上环境 FGC 频繁,如何解决?
Switch to router technology: OSPF single zone configuration, OSPF multi zone and end zone
How to build an automated testing framework?
迁移服务器,重新配置数据库(数据库无监听,启动监听报TNS-12545、TNS-12560、TNS-00515错误)
The first open source MySQL native HTAP database in China will be released soon! Look at the three highlights first, limited to the surrounding areas, waiting for you~
@Autowired注解的原理