spring Bean自动装配
0-0准备:
Cat类:
public class Cat {
public void show() {
System.out.println ("喵喵...");
}
Dog类
public class Dog {
public void show() {
System.out.println ("汪汪...");
}
}
People 类
public class People {
private Cat cat;
private Dog dog;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
1-1 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
bean标签 id唯一标识符 也就相当于我们 学的对象
class 对应对象的全限定名 包名 + 类型
name 也是别名 而且name 可以取多个别名 可以用 , ; 空格分隔
作用域:
scope="singleton" 单例模式
scope="prototype" 原型模式 每次在容器get 都会产生一个新对象
-->
<bean class="com.kuang.w.pojo.Dog" id="dog"/>
<bean class="com.kuang.w.pojo.Cat" id="cat"/>
<!--自动装配autowire="byName 根据类中的set后面的属性 id必须唯一 并且这个bean需要和自动注入的属性的set方法的值一致
autowire="byType" 根据类中的类型与对应属性一致 class必须唯一
-->
<bean class="com.kuang.w.pojo.People" id="people">
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
</bean>
</beans>
1-2 byType自动装配
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
bean标签 id唯一标识符 也就相当于我们 学的对象
class 对应对象的全限定名 包名 + 类型
name 也是别名 而且name 可以取多个别名 可以用 , ; 空格分隔
作用域:
scope="singleton" 单例模式
scope="prototype" 原型模式 每次在容器get 都会产生一个新对象
-->
<bean class="com.kuang.w.pojo.Dog" id="dog"/>
<bean class="com.kuang.w.pojo.Cat" id="cat"/>
<!--
autowire="byType" 根据类中的类型与对应属性一致 class必须唯一
-->
<bean class="com.kuang.w.pojo.People" id="people" autowire="byType">
</bean>
</beans>
1-3 byName自动装配
<!--自动装配autowire="byName 根据类中的set后面的属性 id必须唯一 并且这个bean需要和自动注入的属性的set方法的值一致-->
<bean class="com.kuang.w.pojo.People" id="people" autowire=" byNamee">
</bean>
1-4测试
import com.kuang.w.pojo.People;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest04 {
@Test
public void MT04() {
ApplicationContext context = new ClassPathXmlApplicationContext ("beans.xml");
People people = context.getBean ("people", People.class);
people.getDog ().show ();
people.getCat ().show ();
}
}
结果:
汪汪...
喵喵...
Process finished with exit code 0
注解配置
jdk 1.5支持 spring 2.5支持
1导入context约束
2 配置注解支持
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解的支持-->
<context:annotation-config/>
</beans>
注意重点
注解使用反射 实现 set方法都可以不要
<!--开启注解的支持-->
<context:annotation-config/>
**
@Autowired
**
使用Autowired我们可以不用编写Set方法了,前提是你这个自动装配的属性在l0C (Spring) 容器中存在,且符
合名字byName!
@Autowired
private Cat cat;
@Autowired
private Dog dog;
-
如果用 @Autowired自动配置的环境比较复杂 ,自动装配无法通过一个 @Autowired实现。
-
则使用@Qualifier(value=“xxx”)去配置 @Autowired的使用 指定唯一的bean对象
例子:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210422090448921.png
bean内部的配置
<bean class="com.kuang.w.pojo.Dog" id="dog11"/>
<bean class="com.kuang.w.pojo.Dog" id="dog22"/>
<bean class="com.kuang.w.pojo.Cat" id="cat11"/>
<bean class="com.kuang.w.pojo.Cat" id="cat22"/>
java代码的配置:
@Autowired
@Qualifier(value = "cat22")
private Cat cat;
@Autowired
@Qualifier(value = "dog11")
private Dog dog;
@Autowired
@Qualifier(value = "cat22")组合使用
当 @Autowired自动装配的环境比较复杂,自动装配无法通过一个一个注解【@Autowired】完成时,可以使用@Qualifier(value = "xxx")去配置 @Autowired的使用,指定一个唯一的bean对象注入
import javax.annotation.Resource;
@Resource jdk自带的注解
@Resource 和 @Autowired的区别
-
都是来自动装配 都可以放在属性字段上
-
@Autowired 通过byType实现 而且这个对象必须存在(常用)
-
@Resource默认通过byname的方式实现,如果找不到名字,则通过bytype实现,如果两个都找不到的情况下 就报错(常用)
-
执行顺序不同 @Autowired 通过byType实现 @Resource默认通过byname的方式实现
使用注解开发
1 配置
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.huang.w.dao"/>
<context:annotation-config/>
<!--
bean标签 id唯一标识符 也就相当于我们 学的对象
class 对应对象的全限定名 包名 + 类型
name 也是别名 而且name 可以取多个别名 可以用 , ; 空格分隔
作用域:
scope="singleton" 单例模式
scope="prototype" 原型模式 每次在容器get 都会产生一个新对象
-->
</beans>
2 属性注入
2-0
@Value("王五")
public void setName(String name) {
this.name = name;
}
2-1
@Value("王五")
private String name;
3衍生注解
下面的4个功能一样 用来标注不同层
@Component 有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!
- dao [@Repository]
- service [@Service]
- controller [@ controller ]
- 都是代表将某个类注册到Spring中,装配Bean 交给spring容器中
4 实例
* Component 注册到Spring中
* @Scope("singleton")单例模式
* scope="singleton" 单例模式
scope="prototype" 原型模式 每次在容器get 都会产生一个新对象
*/
@Component
@Scope("singleton")
public class User {
private String name;
private int age;
5.小结
xml与注解:
- 。xml 更加万能,适用于任何场合!维护简单方便 。
- 注解不是自己类使用不了,维护相对复杂!
- xml与注解最佳实践:|
xml用来管理bean; 注解只负责完成属性的注入;
使用java的配置spring
我们现在要完全不使用Spring的xm|配置了,全权交给Java来做!
1.实体类
package com.kuang.w.pojo;
import org.springframework.beans.factory.annotation.Value;
import sun.misc.Contended;
@Contended
public class User {
private String name;
public String getName() {
return name;
}
@Value("我")
public void setName(String name) {
this.name = name;
}
}
2 java类 配置代码
package com.kuang.w.config;
import com.kuang.w.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author W
* @ComponentScan 这个也会Spring容器托管,注册创容器中,因为他本米就是一个@Component
* <context:component-scan base-package="com.huang.w.dao"/>==@ComponentScan("com.kuang.w.pojo")
* @Configuration代表这是一个配置类, 就和我们之前看的beans. xmL
*/
@Configuration
@ComponentScan("com.kuang.w.pojo")
public class MyConfig {
/**
* 注册一个bean 就相当于之前写的一个bean标签
* 这个方法名 就相当于bean标签中的id属性
* 这个方法中的返回值 就相当于bean标签中的class属性
*/
@Bean
public User getUser() {
return new User ();
}
}
3测试
import com.kuang.w.config.MyConfig;
import com.kuang.w.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestDay06 {
@Test
public void day06() {
//如果完全使用了配置类方式去做,我们就只能通过AnnotationConfig 1: 下文米获取容器,通过配置类的cLass对象加载!
ApplicationContext context = new AnnotationConfigApplicationContext (MyConfig.class);
User user = context.getBean ("getUser", User.class);
System.out.println (user.getName ());
}
}