Use @Autowired to autowire Beans
@Autowired annotation autowires a single bean attribute with compatible types
Constructors, ordinary fields (even non-public), all methods with parameters can be applied @Authwired annotation
By default, all attributes annotated with @Autowired need to be set. When Spring cannot find a matching bean assembly attribute, an exception will be thrown . If a certain attribute is not allowed to be set, you can set the required attribute annotated with @Autowired False
By default, when there are multiple types of compatible beans in the IOC container, auto- wiring through types will not work. At this time, you can provide the name of the bean in the @Qualifier annotation. Spring allows you to annotate the input parameters of the method @Qualifiter Specify the name of the injected bean
The @Autowired annotation can also be applied to array-type attributes, at this time Spring will automatically wire all matching beans.
The @Autowired annotation can also be applied to collection properties . At this time, Spring reads the type information of the collection and then automatically assembles all compatible beans.
@Autowired annotation is used on java.util.Map , if the key value of the Map is String, then Spring will automatically assemble a Bean compatible with its Map value type, then the name of the bean is used as the key value
package com.learn.spring.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.learn.spring.service.UserService;
/**
* 表现层的组件
*
* @Controller的作用:
* 等价于在xml配置文件中: <bean id="userController" class="com.learn.spring.controller.UserController"></bean>
* @Controller注解默认的id值是类名首字母小写,如果想要自己指定,可以使用value属性来指定
* @Controller(value="id") / @Controller("id")
*/
@Controller
//@Component
public class UserController {
@Autowired(required=false) // 默认情况下,required=true,代表@AutoWired标注的属性必须被装配.
//可以改为required=false,有就装配,没有就算了.
private UserService userService ;
public void login(){
System.out.println("UserController login .....");
userService.Handlelogin();
}
}
package com.learn.spring.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.learn.spring.dao.UserDao;
/**
* 业务层的组件
*
*/
@Service
public class UserService {
/**
*@AutoWired 优先采用类型的匹配的方式进行bean的装配. 如果有多个类型兼容的bean匹配了,会使用
* 属性名 与bean的id值进行匹配.
*/
@Autowired
@Qualifier("userDaoMyBatisImpl") //进一步的指定要装配的bean的id值.
private UserDao userDao ;
public void Handlelogin(){
System.out.println("UserService handleLogin ....");
//处理登录的逻辑
userDao.getUserByUsernameAndPassword();
}
}
package com.learn.spring.dao;
public interface UserDao {
public void getUserByUsernameAndPassword();
}
package com.learn.spring.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoMyBatisImpl implements UserDao{
public void getUserByUsernameAndPassword(){
System.out.println("UserDaoMyBatisImpl getUserByUsernameAndPassword......");
}
}