8_Spring_注解方式管理bean

8_Spring_注解方式管理bean

1注解方式创建对象IOC

导入依赖 aop

@Component    放在类上,用于标记,告诉spring当前类需要由容器实例化bean并放入容器中

该注解有三个子注解

@Controller   用于实例化controller层bean

@Service        用于实例化service层bean

@Repository  用于实例化持久层bean

当不确定是哪一层,就用Component

这几个注解互相混用其实也可以,但是不推荐

第一步:在applicationContext.xml中配置开启注解扫描

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:c="http://www.springframework.org/schema/c"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. ">
  11. <!--添加注解扫描,扫描指定的包,将包中的所有有注解的类实例化
  12. base-package 后面放要扫描的包
  13. 如果有多个包需要扫描,可以使用逗号隔开 com.msb.bean,com.msb.service
  14. 或者可以写上一层包路径 com.msb
  15. 可以通过注解指定bean的id@Component("user1")
  16. 如果不指定,则id默认是 类名首字母小写
  17. -->
  18. <context:component-scan
    base-package="com.msb.bean"></context:component-scan>

第二步:在类上添加注解,让spring容器给我们创建bean实例并存储于容器中

  1. package com.msb.bean;
  2. import org.springframework.stereotype.Component;
  3. /**
    • @Author: Ma HaiYang
    • @Description: MircoMessage:Mark_7001
  4. */
  5. @Component(value = "user1")
  6. public class User {
  7. }

测试代码

  1. package com.msb.test;
  2. import com.msb.bean.User;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. /**
    • @Author: Ma HaiYang
    • @Description: MircoMessage:Mark_7001
  7. */
  8. public class Test1 {
  9. @Test
  10. public void testGetBean(){
  11. ApplicationContext context =new
    ClassPathXmlApplicationContext("applicationContext.xml");
  12. User user = context.getBean("user1", User.class);
  13. System.out.println(user);
  14. }
  15. }

组件扫描配置注解识别

  1. <context:component-scan base-package="com.msb"
    use-default-filters="false">
  2. <context:include-filter type="annotation"
    expression="org.springframework.stereotype.Controller"/>
  3. </context:component-scan>
  4. <context:component-scan base-package="com.msb"
    use-default-filters="true">
  5. <context:exclude-filter type="annotation"
    expression="org.springframework.stereotype.Controller"/>
  6. </context:component-scan>

2注解方式依赖注入DI

@Autowired   根据属性数据类型自动装配

@Qualifier      根据属性名称注入依赖

@Resources   可以根据类型,也可以根据名称注入

@Value           注入普通数据类型(8+String)

项目结构如下

image

applicationContext.xml中配置包扫描和读取属性配置文件

Dao层

接口

image
实现类

image

image
让容器扫描 Service层,实例化对象

接口

image
实现类

  1. package com.msb.service.impl;
  2. import com.msb.dao.UserDao;
  3. import com.msb.service.UserService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Qualifier;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.stereotype.Service;
  8. import javax.annotation.Resource;
  9. /**
    • @Author: Ma HaiYang
    • @Description: MircoMessage:Mark_7001
  10. */
  11. @Service
  12. public class UserServiceImpl implements UserService {
  13. /*
  14. *@Autowired
  15. * 根据类型到容器中去寻找对应的对象,找到后给当前属性赋值
  16. * 不需要依赖 set方法
  17. * 属性类型可以是接口,会自动匹配对应的实现类对象
  18. * @Autowired配合 @Qualifier,可以通过名称指定注入的对象
  19. *
  20. * @Resource 如果不配置name 那么就是根据类型注入
  21. * @Resource(name="userDaoImplB") 配置name,就是根据名称注入
  22. *
  23. *
  24. * @Resource 是JDK中javax包的注解
  25. * @Autowired 和 @Qualifier 是spring中的注解
  26. *
  27. * @Value 可以个普通属性赋值
  28. * @Value 可以使用${}这种表达式获取系统的变量值
  29. * 或者是.properties属性配置文件中的值
  30. *
  31. * */
  32. //@Autowired
  33. //@Qualifier("userDaoImplA")
  34. //@Qualifier("userDaoImplB")
  35. //private UserDao userDao ;
  36. @Resource(name="userDaoImplB")
  37. private UserDao userDao ;
  38. @Value("${username}")
  39. private String sname;
  40. @Value("boy")
  41. private String sgender;
  42. @Value("${age}")
  43. private Integer sage;
  44. @Override
  45. public void add() {
  46. System.out.println("userServiceImpl add ... ... ");
  47. System.out.println(sname);
  48. System.out.println(sgender);
  49. System.out.println(sage);
  50. userDao.add();
  51. }
  52. }

测试代码

  1. package com.msb.test;
  2. import com.msb.service.UserService;
  3. import org.junit.Test;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. /**
    • @Author: Ma HaiYang
    • @Description: MircoMessage:Mark_7001
  7. */
  8. public class Test1 {
  9. @Test
  10. public void testGetBean(){
  11. ApplicationContext context =new
    ClassPathXmlApplicationContext("applicationContext.xml");
  12. UserService userService = context.getBean("userServiceImpl",
    UserService.class);
  13. userService.add();
  14. }
  15. }

3完全使用注解(了解)

创建配置类,替代XML配置文件

image
测试代码

  1. @Test
  2. public void testGetBean2(){
  3. ApplicationContext context=new
    AnnotationConfigApplicationContext(SpringConfig.class);
  4. UserServiceImpl userService = context.getBean("userServiceImpl",
    UserServiceImpl.class);
  5. userService.add();
  6. }

Generated with Mybase Desktop 8.2.13

posted @ 2023-07-30 12:36  AidenDong  阅读(40)  评论(0)    收藏  举报