7、Bean的自动装配
- 自动装配是Spring满足bean依赖的一种方式
- Spring会在上下文中自动寻找,并自动给beanh装配属性!
三种装配的方式:
- 在xml中显示配置
- 在Java中显示配置
- 隐式的自动装配bean【重要】
7.1、测试
1.搭建环境
一个人有两个宠物!
- ByName
<!--bean id要与需要自动装配的类的属性名对应-->
<bean id="cat" class="com.cjp.pojo.Cat" ></bean>
<bean id="dog" class="com.cjp.pojo.Dog" ></bean>
<bean id="person" class="com.cjp.pojo.Person" autowire="byName" >
<property name="name" value="cjp"/>
</bean>
- ByType
<!--bean id可以不要要与需要自动装配的类的属性名对应 类型相对应就行了-->
<bean id="ca" class="com.cjp.pojo.Cat" ></bean>
<bean id="do" class="com.cjp.pojo.Dog" ></bean>
<bean id="person" class="com.cjp.pojo.Person" autowire="byType" >
<property name="name" value="cjp"/>
</bean>
- 使用注解自动装配
1.导入约束
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>
3.@Autowired
当容器中的bean类型与Person类属性唯一对应时,可以只是用@Autowired
@Autowired可以在实体类属性上使用,也可以在setter方法上使用;在属性上使用后可以不用写setter方法了。
Person:
public class Person {
@Autowired
private Dog dog;
@Autowired
private Cat cat;
private String name;
//getter setter 方法省略
}
beans.xml:
<bean id="cat" class="com.cjp.pojo.Cat" ></bean>
<bean id="dog" class="com.cjp.pojo.Dog" ></bean>
<bean id="person" class="com.cjp.pojo.Person" />
4.@Qualifier
当容器中同一个类有多个实例是 ,注入时就存在争议,这是仅仅使用@Autowired就不行了
还需要使用@Qualifier来制定要注入的实例名
<bean id="cat111" class="com.cjp.pojo.Cat" ></bean>
<bean id="cat11" class="com.cjp.pojo.Cat" ></bean>
<bean id="dog111" class="com.cjp.pojo.Dog" ></bean>
<bean id="dog11" class="com.cjp.pojo.Dog" ></bean>
<bean id="person" class="com.cjp.pojo.Person" />
public class Person {
@Autowired
@Qualifier(value = "dog11")
private Dog dog;
@Autowired
@Qualifier(value = "cat111")
private Cat cat;
private String name;
}
5.@Resource
public class Person {
@Resource(name = "dog11")
private Dog dog;
@Resource(name = "cat111")
private Cat cat;
private String name;
}
小结:
@Resource和@Autowired的区别:
- 都是用来自动装配的,都可以放在属性字段上
- @Autowired通过ByType的方式实现
- @Resource默认通过ByName的方式实现,如果找不到名字,则通过ByType实现,若果两个都找不到就报错!
- 执行顺序不同:@Autowired通过ByType的方式实现。
8、使用注解开发
在Spring4
之后之后,要是用注解开发,必须要保证导入了AOP的包。
使用注解需要导入context约束,增加注解的支持。
1、bean
-
导入约束
<context:component-scan base-package="com.cjp.pojo"/>
-
使用注解
```java package com.cjp.pojo; import org.springframework.stereotype.Component; //相当于<bean id="user" class="com.cjp.pojo.User"/> @Component public class User { public String name="cjp"; } ```
2、属性如何注入
@Component
public class User {
/* 相当于<bean id="user" class="com.cjp.pojo.User">
<property name="name" value="cjp"/>
</bean>*/
@Value("cjp")
public String name;
}
3、衍生的注解
@Component有几个衍生的注解,我们在web开发中,会按照MVC三层架构分层!
- dao 【@Repository】
- service 【@Service】
- controller 【@Controller】
这四个注解功能都一样,都是将某个类注册到Spring容器中!
4、自动装配
@Autowired:自动装配通过名字、类型
若果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value="xxx")
@Nullable 字段标记了这个注解,说明这个字段可以为null
@Resource:自动装配通过名字、类型
5、作用域
@Scope("prototype")
public class User {
/* 相当于<bean id="user" class="com.cjp.pojo.User">
<property name="name" value="cjp"/>
</bean>*/
@Value("cjp")
public String name;
}
6、小结
xml与注解:
- xml更加万能,适用于任何场合!维护简单
- 注解 不是自己类使用不了,维护相对复杂
xml与注解的最佳实践:
- xml用来管理bean;
- 注解只负责完成属性的注入;
- 我们在使用的过程中,要注意开启注解支持
9、使用java 的方式配置Spring
使用java配置类来取代xml配置文件。
JavaConfig是Spring的一个子项目,在Spring4之后,他成为了一个核心功能!
1.实体类
package com.cjp.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//表明这个类被Spring接管了,注册到了容器中
@Component
public class User {
//注入属性
@Value("cjp")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
2.配置类MyConfig
package com.cjp.config;
import com.cjp.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration//代表这是一个配置类,相当于beans.xml
//这个也会被Spring容器托管,注册到容器中,因为他本来就是一个@Component
@ComponentScan("com.cjp.pojo")
@Import(MyConfig2.class) //导入其他配置类
public class MyConfig {
/*相当于beans.xml中的bean标签
* 方法名就相当于标签中的id
* 方法的返回值就相当于标签中的class*/
@Bean
public User getUser(){
return new User();//返回要注入到bean的对象
}
}
3.测试类
import com.cjp.config.MyConfig;
import com.cjp.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
User getUser = context.getBean("getUser", User.class);
System.out.println(getUser.getName());
}
}
这种纯java的配置方式,在SpringBoot中随处可见!
10、代理模式
代理模式是SpringAOP的底层!
代理模式的分类:
- 静态代理
真实角色(Real)
代理角色(proxy)
客户端(Client)
-
动态代理
- 动态代理角色和静态代理角色一样
- 动态代理的代理类时动态生成的,不是我们直接写好的
- 动态代理分为两大类:基于接口的动态代理,基于类的动态代理
- 基于接口:JDK动态代理 【我们在这里使用的】
- 基于类:Cglib
- java字节码实现:JAVAsist
1.接口
package com.cjp.demo01; public interface Rent { public void rent(); }
2.真实角色
package com.cjp.demo01; public class Host implements Rent{ @Override public void rent() { System.out.println("房东出租房屋!"); } }
3.生成动态代理类的工具类
package com.cjp.demo01; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class ProxyInvokeHandler implements InvocationHandler { //被代理的接口 private Object target; public void setRent(Object target) { this.target = target; } //生成得到代理类 public Object getProxy(){ return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this); } //处理代理实例,并返回结果 @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = method.invoke(target, args); return result; } }
4.测试类
package com.cjp.demo01; import java.lang.reflect.Proxy; public class Client { public static void main(String[] args) { Host host = new Host();//真实角色 ProxyInvokeHandler pih = new ProxyInvokeHandler(); pih.setRent(host); Rent proxy = (Rent) pih.getProxy(); proxy.rent(); } }
动态代理类代理的是一个接口,所以可以代理多个实现类,只需要修改注入的实现类即可
11.AOP实现
导入依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
方式一
1.接口
2.接口实现类
3.日志类
package com.cjp.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class After implements AfterReturningAdvice {
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("使用了"+method.getName()+"方法,返回值为:"+o);
}
}
package com.cjp.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Before implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("使用了"+o.getClass().getName()+"类的"+method.getName()+"方法");
}
}
4.beans配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.cjp.service.UserServiceImpl"/>
<bean id="before" class="com.cjp.log.Before"/>
<bean id="after" class="com.cjp.log.After"/>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.cjp.service.UserServiceImpl.*(..))"/>
<aop:advisor advice-ref="after" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="before" pointcut-ref="pointcut"/>
</aop:config>
</beans>
5.测试
import com.cjp.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.select();
}
}