Spring IOC container bean management-based on annotation method

IOC operation bean management (based on annotation method)

1. What is a comment

​ (1) Annotation is a special code mark, format: @Annotation name (attribute name=attribute value, attribute name=attribute value...)

​ (2) Use annotations, which act on classes, methods, and attributes

​ (3) Purpose of using annotations: simplify xml configuration

2, Spring provides annotation management for creating objects Bean

​ The following four annotation functions are the same, all can be usedCreate bean instance

​ (1)@Component

​ (2)@Service

​ (3)@Controller

​ (4)@Repository

3, annotation-based way to achieve the object creation

​ The first step is to introduce dependencies (introducing spring-aop jar package )

​ The second step is to turn on the component scan

<!--开启组件扫描
 1 如果扫描多个包,多个包使用逗号隔开
 2 扫描包上层目录
-->
<context:component-scan base-package="com.atguigu"></context:component-scan>

​ The third step is to create a class and add an annotation to create an object on the class

//在注解里面 value 属性值可以省略不写,
//默认值是类名称,首字母小写
//UserService -- userService
@Component(value = "userService") //注解等同于XML配置文件:<bean id="userService" class=".."/>
public class UserService {
    
    
 public void add() {
    
    
 System.out.println("service add.......");
 }
}

4, details of the configuration scanning assembly opening

<!--示例 1
 use-default-filters="false" 表示现在不使用默认 filter,自己配置 filter
 context:include-filter ,设置扫描哪些内容
-->
<context:component-scan base-package="com.atguigu" use-defaultfilters="false">
 <context:include-filter type="annotation"

expression="org.springframework.stereotype.Controller"/><!--代表只扫描Controller注解的类-->
</context:component-scan>
<!--示例 2
 下面配置扫描包所有内容
 context:exclude-filter: 设置哪些内容不进行扫描
-->
<context:component-scan base-package="com.atguigu">
 <context:exclude-filter type="annotation"

expression="org.springframework.stereotype.Controller"/><!--表示Controller注解的类之外一切都进行扫描-->
</context:component-scan>

5, annotation-based mannerAttribute injection

​ (1) @Autowired: Automatic assembly according to attribute type

​ The first step is to create the service and dao objects, and add the creation object annotations to the service and dao classes

The second step is to inject the dao object into the service, add the dao type attribute to the service class, and use annotations on the attributes

@Service
public class UserService {
    
    
 //定义 dao 类型属性
 //不需要添加 set 方法
 //添加注入属性注解
 @Autowired
 private UserDao userDao;
 public void add() {
    
    
 System.out.println("service add.......");
 userDao.add();
 }
}

//Dao实现类
@Repository
//@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao {
    
    
    @Override
    public void add() {
    
    
        System.out.println("dao add.....");
    }
}

​ (2) @Qualifier: Inject according to the name, the use of this @Qualifier annotation is used together with @Autowired above

//定义 dao 类型属性
//不需要添加 set 方法
//添加注入属性注解
@Autowired //根据类型进行注入
//根据名称进行注入(目的在于区别同一接口下有多个实现类,根据类型就无法选择,从而出错!)
@Qualifier(value = "userDaoImpl1") 
private UserDao userDao;

​ (3) @Resource: It can be injected according to the type, or according to the name (it belongs to the annotation under the javax package and is not recommended!)

//@Resource //根据类型进行注入
@Resource(name = "userDaoImpl1") //根据名称进行注入
private UserDao userDao;

​ (4) @Value: Inject common type attributes

@Value(value = "abc")
private String name

6, fully annotated Development

​ (1) Create a configuration class to replace the xml configuration file

@Configuration //作为配置类,替代 xml 配置文件
@ComponentScan(basePackages = {
    
    "com.atguigu"})
public class SpringConfig {
    
    
    
}

​ (2) Write test class

@Test
public void testService2() {
    
    
 //加载配置类
 ApplicationContext context
 = new AnnotationConfigApplicationContext(SpringConfig.class);
 UserService userService = context.getBean("userService",
UserService.class);
 System.out.println(userService);
 userService.add();
}

Guess you like

Origin blog.csdn.net/weixin_45496190/article/details/107071204