2.Spring学习之路(自动装配、基于注解开发)

一.Bean的自动装配

1.1三种装配机制
(1).xml中显示配置
(2).自动装配
(3)java中显示配置

1.2自动装配的两个操作
组件扫描,自动装配
DEMO

public class Cat {
    public void shout() {
        System.out.println("miao~");
    }
}
public class Dog {
    public void shout() {
        System.out.println("wang~");
    }
}
public class User {
    private Cat cat;
    private Dog dog;
    private String str;
}

1.3基于配置文件的自动装配
ByName(按名称自动装配)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dog" class="com.kuang.pojo.Dog"/>
    <bean id="cat" class="com.kuang.pojo.Cat"/>

    <bean id="user" class="com.kuang.pojo.User" autowire="byName">
    <property name="str" value="qinjiang"/>
</bean>
</beans>

如果把cat的bean id改成cat111,就会报空指针,小结如下:
1.将查找其类中所有的set方法名,例如setCat,获得将set去掉并且首字母小写的字符串,即cat。
2.去spring容器中寻找是否有此字符串名称id的对象。
3.如果有,就取出注入;如果没有,就报空指针异常。

byType(按类型自动装配)小结如下:
1.将user的bean配置修改一下 : autowire=“byType”
2.如果spring容器中类型不唯一,那么就会报不唯一的异常
1.4.基于注解的自动装配
1.准备工作,配置文件引入context文件头

xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd

2.开启注解支持

<context:annotation-config/>

3.代码测试

public class User {
    @Autowired
    private Cat cat;
    @Autowired
    private Dog dog;
    private String str;

    public Cat getCat() {
        return cat;
    }
    public Dog getDog() {
        return dog;
    }
    public String getStr() {
        return str;
    }
}
<context:annotation-config/>

<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="user" class="com.kuang.pojo.User"/>

二.基于注解开发

2.1 要引入context约束

<?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: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/context
        http://www.springframework.org/schema/context/spring-context.xsd">

</beans>

2.2 Bean的实现
1.配置扫描的包

<!--指定注解扫描包-->
<context:component-scan base-package="com.kuang.pojo"/>

2.在指定包下增加注解和属性注入

@Component("user")
// 相当于配置文件中 <bean id="user" class="当前注解的类"/>
public class User {
    @Value("秦疆")
    // 相当于配置文件中 <property name="name" value="秦疆"/>
    public String name;
}

基于注解的ioc配置和xml配置要实现的功能都是一样的,都是要降低程序间的耦合,只是配置的形式不一样。
使用@Componet注解来配置管理的资源(即产生bean)
账户的业务层实现类

@Component("accountService") 
public class AccountServiceImpl implements IAccountService {    
private IAccountDao accountDao; 
 public void setAccountDao(IAccountDao accountDao) {   
 this.accountDao = accountDao;  
	 }
 } 

账户的持久层实现类

 @Component("accountDao")
     public class AccountDaoImpl  implements IAccountDao { 
     private DBAssit dbAssit; }

当我们基于注解整合时,导入约束的时候需要多导入一个context名称空间下的约束
告知 spring 创建容器时要扫描的包
<context:component-scan base-package="com.itheima"></context:component-scan>
@Component属性有:value:指定bean的id,如果不指定value属性,默认是bean的id当前类的类名,首字母小写,然后和@component等价的注解有@Controller,@Service,@Repository,然后他们分别用在表现层,业务层,持久层

注入数据
相当于,
@Aurowired
作用: 自动按照类型注入。当使用注解注入属性时,set方法可以省略。它只能注入其他 bean 类型。当有多个 类型匹配时,使用要注入的对象变量名称作为 bean 的 id,在 spring 容器查找,找到了也可以注入成功。找不到 就报错。
@Qualifier
作用: 在自动按照类型注入的基础之上,再按照 Bean 的 id 注入。它在给字段注入时不能独立使用,必须和 @Autowired一起使用;但是给方法参数注入时,可以独立使用
属性: value:指定 bean 的 id。
@Resource
作用: 直接按照 Bean 的 id 注入。它也只能注入其他 bean 类型。
属性: name:指定 bean 的 id。
@Value
作用: 注入基本数据类型和 String 类型数据的
属性: value:用于指定值
改变作用范围

相当于:<bean id="" class="" scope=""> 

@Scope
作用: 指定 bean 的作用范围。
属性: value:指定范围的值。
取值:singleton prototype request session globalsession

和生命周期相关

相当于:<bean id="" class="" init-method="" destroy-method="" /> 

@PostConstruct
作用: 用于指定初始化方法。
@PreDestroy
作用: 用于指定销毁方法。

参考基于注解开发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值