spring设值注入-构造注入
创建实体类,并实现构造方法
public class Star {
private String name;
private int age;
private Partner partner;
public Star() {
super();
}
//带参构造器
public Star(String name, int age, Partner partner) {
super();
this.name = name;
this.age = age;
this.partner = partner;
}
@Override
public String toString() {
return "Star [name=" + name + ", age=" + age + ", partner=" + partner + "]";
}
}
关于构造器注入三种方式
<?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">
<!-- 构造方式DI -->
<!--
<bean id="star" class="com.bjsxt.pojo.Star">
<constructor-arg name="name" value="郭靖"></constructor-arg>
<constructor-arg name="age" value="25"></constructor-arg>
<constructor-arg name="partner" ref="partner"></constructor-arg>
</bean>
-->
<!--
<bean id="star" class="com.bjsxt.pojo.Star">
<constructor-arg index="0" value="郭靖"></constructor-arg>
<constructor-arg index="1" value="25"></constructor-arg>
<constructor-arg index="2" ref="partner"></constructor-arg>
</bean>
-->
<bean id="star" class="com.bjsxt.pojo.Star">
<constructor-arg value="郭靖"></constructor-arg>
<constructor-arg value="25"></constructor-arg>
<constructor-arg ref="partner"></constructor-arg>
</bean>
<bean id="partner" class="com.bjsxt.pojo.Partner">
<property name="name" value="维多利亚"></property>
</bean>
</beans>
测试类:
public class SomeTest {
@Test
public void someTest01(){
//创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Star star = ac.getBean("star", Star.class);
System.out.println(star);
}
}
jdk动态代理
public class SomeTest {
public static void main(String[] args) {
//定义目标对象
final SomeService target = new SomeServiceImpl();
//定义目标对象的代理对象
SomeService proxy = (SomeService) Proxy.newProxyInstance(target.getClass().getClassLoader(),//目标类的类加载器
target.getClass().getInterfaces(),//目标类实现的所有接口
new InvocationHandler() {//调用处理器
//proxy:代理对象
//method:目标方法
//args:目标方法参数
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String result = (String) method.invoke(target, args);
return result.toUpperCase();
}
});
String result1 = proxy.doSome();
System.out.println(result1);
}
}
代理类及其实现类
package com.bjsxt.service.impl;
import com.bjsxt.service.SomeService;
//目标类
public class SomeServiceImpl implements SomeService {
public SomeServiceImpl() {
System.out.println("无参构造器执行!");
}
@Override
public String doSome() {
return "China";
}
}
package com.bjsxt.service;
//主业务接口
public interface SomeService {
String doSome();
}
CGLIB动态代理
使用CGLIB动态代理的实现步骤
1、引入cglib的jar包
2、创建动态代理工厂,并实现MethodIntercepter接口
3、创建cglib代理对象的方法
4、创建增强器
5、指定目标类
6、指定回调接口
7、创建代理对象
代码:
//cglib动态代理工厂
public class CglibProxyFactory implements MethodInterceptor{
private SomeServiceImpl target;
public CglibProxyFactory() {
super();
}
public CglibProxyFactory(SomeServiceImpl target) {
super();
this.target = target;
}
//创建cglib代理对象的方法
public SomeServiceImpl proxyCreator(){
//创建增强器
Enhancer enhancer = new Enhancer();
//指定父类(指定目标类)
enhancer.setSuperclass(SomeServiceImpl.class);
//指定回调接口对象
enhancer.setCallback(this);
//创建cglib代理对象
return (SomeServiceImpl) enhancer.create();
}
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
String result = (String) method.invoke(target, args);
return result.toUpperCase();
}
}
//目标类
public class SomeServiceImpl {
public SomeServiceImpl() {
System.out.println("无参构造器执行!");
}
public String doSome() {
return "China";
}
}
测试类
public class SomeTest {
public static void main(String[] args) {
//定义目标对象
SomeServiceImpl target = new SomeServiceImpl();
//定义目标对象的代理对象
SomeServiceImpl proxy = new CglibProxyFactory(target).proxyCreator();
String result1 = proxy.doSome();
System.out.println(result1);
}
}
AOP:面向切面编程,就是将交叉业务逻辑封装成切面,利用aop的功能将切面置入到主业务逻辑中,所谓的交叉业务逻辑就是指通用的,与主业务逻辑无关的代码,如安全检查,事务,日志等
aop前置通知:
//切面:前置通知
public class MyMethodBeforeAdvice implements MethodBeforeAdvice {
/**
* method:目标方法
* args:目标方法参数列表
* target:目标对象
*/
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("前置通知的before()方法执行!");
}
}
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 注册目标类 -->
<bean id="someServiceImpl" class="com.bjsxt.service.impl.SomeServiceImpl"></bean>
<!-- 注册切面:前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.bjsxt.aspects.MyMethodBeforeAdvice"></bean>
<!-- 注册代理 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 指定目标对象 -->
<property name="target" ref="someServiceImpl"></property>
<!-- 指定目标类实现的所有接口 -->
<property name="interfaces" value="com.bjsxt.service.SomeService"></property>
<!-- 指定切面 -->
<property name="interceptorNames" value="myThrowsAdvice"></property>
</bean>
</beans>
其他的都是一样的流程:只不不过实现的接口不一样
//切面:后置通知
public class MyAfterReturningAdvice implements AfterReturningAdvice {
/**
* returnValue:目标方法的返回值
*/
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("后置通知的afterReturning()方法执行!returnValue:"+ returnValue);
if(returnValue!=null){
System.out.println("后置通知的afterReturning()方法执行!returnValue:"+((String)returnValue).toUpperCase());
}
}
}
//切面:环绕通知
public class MyMethodInterceptor implements MethodInterceptor {
/**
* invocation:方法调用器
*/
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("环绕通知:目标方法执行之前!");
//调用执行目标方法
Object result = invocation.proceed();
if(result!=null){
result = ((String)result).toUpperCase();
}
System.out.println("环绕通知:目标方法执行之后!");
return result;
}
}
//切面:异常通知
public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Exception ex){
System.out.println("异常通知执行!");
}
}
<?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="someServiceImpl" class="com.bjsxt.service.impl.SomeServiceImpl"></bean>
<!-- 注册切面:前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.bjsxt.aspects.MyMethodBeforeAdvice"></bean>
<!-- 注册切面:后置通知 -->
<bean id="myAfterReturningAdvice" class="com.bjsxt.aspects.MyAfterReturningAdvice"></bean>
<!-- 注册切面:环绕通知 -->
<bean id="myMethodInterceptor" class="com.bjsxt.aspects.MyMethodInterceptor"></bean>
<!-- 注册切面:异常通知 -->
<bean id="myThrowsAdvice" class="com.bjsxt.aspects.MyThrowsAdvice"></bean>
<!-- 注册代理 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 指定目标对象 -->
<property name="target" ref="someServiceImpl"></property>
<!-- 指定目标类实现的所有接口 -->
<property name="interfaces" value="com.bjsxt.service.SomeService"></property>
<!-- 指定切面 -->
<property name="interceptorNames" value="myThrowsAdvice"></property>
</bean>
</beans>