Spring框架(2)

标题注解方式管理bean

1、注解方式创建对象IOC

导入依赖 aop(在导入依赖context时带有它)

@Component 放在类上,用于标记,告诉spring当前类需要由容器实例化bean并放入容器中
该注解有三个子注解
@Controller 用于实例化controller层bean
@Service 用于实例化service层bean
@Repository 用于实例化持久层bean
当不确定是哪一层,就用Component
这几个注解互相混用其实也可以,但是不推荐
第一步:在applicationContext.xml中配置开启注解扫描

 <!--
	xmlns:context="http://www.springframework.org/schema/context"
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
	
    添加注解扫描,扫描指定的包,将包中的所有有注解的类实例化
    base-package 后面放要扫描的包
    如果有多个包需要扫描,可以使用逗号隔开  com.msb.bean,com.msb.service
    或者可以写上一层包路径  com.msb
    可以通过注解指定bean的id@Component("user1")
    如果不指定,则id默认是 类名首字母小写
    -->
    <!--
    use-default-filters="true"
    默认值为true 代表使用默认的扫描过滤器
    默认的扫描过滤器会识别并包含 @Component @Controller @Service @Repository 四个注解
    不使用默认的filter,使用我们自己的filter
    include-filter 控制只扫描哪些IOC注解
    exclude-filter 控制不扫描哪些IOC注解
    -->
    <!--组件扫描配置注解识别-->
    <!--控制只扫描Controller注解-->
    <context:component-scan base-package="com.wml.Bean" use-default-filters="true">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--控制不扫描Service注解-->
    <context:component-scan base-package="com.wml.Bean" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>

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

//@Component("user1")
@Component
//@Controller
//@Service
//@Repository
public class User {   }

第三步:测试代码

public class Test01 {
    @Test
    public void testAnnotation(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml");
        User user1 = applicationContext.getBean("user", User.class);
        System.out.println(user1);
    }
}

2、注解方式依赖注入DI

使用到注解有:
@Autowired 根据属性数据类型自动装配
@Qualifier 根据属性名称注入依赖
@Resources 可以根据类型,也可以根据名称注入
@Value 注入普通数据类型(8+String)

注:IDEA中继承标签@Override消失不生效的解决方案
原因:idea中默认的Language Level使用的是5.0版本,不支持在重写的接口方法上加@Override注解。
解决办法:File—Project Structure—Modules—Sources—Language Level改成5以上的版本
在这里插入图片描述
项目结构如下:在这里插入图片描述
Dao层
接口:

public interface UserDao {
    void add();
}

实现类:

@Repository
public class UserDaoImplA implements UserDao {
    @Override
    public void add() {
        System.out.println("UserDaoImplA add... ...");
    }
}
@Repository
public class UserDaoImplB implements UserDao {
    @Override
    public void add() {
        System.out.println("UserDaoImplB add... ...");
    }
}

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
">
	<!--读取aaa.properties属性配置文件-->
    <context:property-placeholder location="classpath:aaa.properties"></context:property-placeholder>

    <!--添加注解扫描,扫描指定的包,将包中的所有有注解的类实例化-->
    <context:component-scan base-package="com.wml" >
    </context:component-scan>
</beans>

aaa.properties属性配置文件
注:解决中文乱码方案
File—Sttings—Editor—File Encodings
在这里插入图片描述

name=xiaobai
sgender=nan
sage=16

让容器扫描 Service层,实例化对象
Service层
接口:

public interface UserService {
    void add();
}

实现类:

@Service
public class UserServiceImpl implements UserService {
    /*
     * @Autowired 实际开发中用的最多
     * 根据类型到容器中去寻找对应的对象,找到后给当前属性赋值
     * 不需要依赖 set方法
     * 属性类型可以是接口,会自动匹配对应的实现类对象
     * @Autowired配合 @Qualifier,可以通过名称指定注入的对象
     *
     * @Resource 如果不配置name 那么就是根据类型注入
     * @Resource(name="userDaoImplB") 配置name,就是根据名称注入
     *
     * @Resource  是JDK中javax包的注解
     * @Autowired 和 @Qualifier 是spring(springframework)中的注解
     *
     * 普通数据类型的属性赋值 :8+string
     * @Value 可以个普通属性赋值
     * @Value 可以使用${}这种表达式获取系统的变量值
     *        或者是.properties属性配置文件中的值
     * */
    @Autowired
    //@Qualifier(value = "userDaoImplA")//可以省略value
    //@Qualifier("userDaoImplB")
    @Resource(name = "userDaoImplA")
    //@Resource(name = "userDaoImplB")
    private UserDao userDao;

    //普通数据类型的属性赋值 :8+string
    /*这样值就写死了
    @Value("小明")
    private String name;
    @Value("男")
    private String sgender;
    @Value("16")
    private Integer sage;*/

    @Value("${name}")
    private String name;
    @Value("${sgender}")
    private String sgender;
    @Value("${sage}")
    private Integer sage;

    @Override
    public void add() {
        System.out.println("UserServiceImpl add... ...");
        System.out.println(name);
        System.out.println(sgender);
        System.out.println(sage);
        userDao.add();
    }
}

测试代码:

public class Test02 {
    @Test
    public void testAnnotation(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext2.xml");
        UserServiceImpl userService = applicationContext.getBean("userServiceImpl", UserServiceImpl.class);
        userService.add();
    }
}

3、配置类方式实现IOC和DI(了解即可)

完全使用注解
创建配置类,替代XML配置文件
在这里插入图片描述

@ComponentScan(basePackages = "com.wml")
@PropertySource("classpath:aaa.properties")
public class SpringConfig {  }

实现类:

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    @Resource(name = "userDaoImplA")
    private UserDao userDao;
    @Value("${name}")
    private String name;
    @Value("${sgender}")
    private String sgender;
    @Value("${sage}")
    private Integer sage;
    @Override
    public void add() {
        System.out.println("UserServiceImpl add... ...");
        System.out.println(name);
        System.out.println(sgender);
        System.out.println(sage);
        userDao.add();
    }
}

aaa.properties属性配置文件:

name=xiaobai
sgender=nan
sage=16

测试代码:

public class Test03 {
    @Test
    public void testAnnotation(){
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
        UserServiceImpl userService = applicationContext.getBean("userServiceImpl", UserServiceImpl.class);
        userService.add();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值