1.两组泛型继承类,和一个实体类
第一组:
package com.test.daselin.generate.di;
public class BaseRespostory {
}
}
package com.test.daselin.generate.di;
import org.springframework.stereotype.Repository;
@Repository
public class UserRepository extends BaseRespostory<User> {
}
//第二组 父类作为泛型注入的链接类
package com.test.daselin.generate.di
import org.springframework.beans.factory.annotation.Autowired
public class BaseService {
@Autowired
protected BaseRespostory repository
public void add(){
System.out.println("add")
System.out.println(repository)
}
}
package com.test.daselin.generate.di;
import org.springframework.stereotype.Service;
@Service
public class UserService extends BaseService<User> {
}
实体类
package com.test.daselin.generate.di;
public class User {
}
测试结果
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.daselin.generate.di.UserService;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-generate.xml");
UserService userService = (UserService) ctx.getBean("userService");
userService.add();
}
}
spring 的bean配置
<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"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<context:component-scan base-package="com.test.daselin.generate.di">context:component-scan>
beans>
结果:
add
com.test.daselin.generate.di.UserRepository@547e045
二、动态代理
1.接口
package com.test.dasenlin.generate.aop;
public interface ArithmeticCalculator {
Integer getPlus(Integer i,Integer j);
Integer getMinus(Integer i,Integer j);
Integer getMultiply(Integer i,Integer j);
Integer getExcept(Integer i,Integer j);
}
2.实现类
package com.test.dasenlin.generate.aop;
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public Integer getPlus(Integer i, Integer j) {
return i+j;
}
@Override
public Integer getMinus(Integer i, Integer j) {
return i-j;
}
@Override
public Integer getMultiply(Integer i, Integer j) {
return i*j;
}
@Override
public Integer getExcept(Integer i, Integer j) {
if(j!=0){
return i/j;
}else{
return -1;
}
}
}
动态代理
package com.test.dasenlin.generate.aop;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
public class ArithmeticCalculatorLoggingCglibProxy {
private ArithmeticCalculator target;
public ArithmeticCalculatorLoggingCglibProxy(ArithmeticCalculator target){
this.target = target;
}
public ArithmeticCalculator getLoggingProxy(){
ArithmeticCalculator proxy = null;
ClassLoader loader = target.getClass().getClassLoader();
Class [] interfaces = new Class [] {ArithmeticCalculator.class};
InvocationHandler h = new InvocationHandler() {
/**
* proxy 正在返回的那个代理对象,一般情况下,在invoke方法中都不使用该对象
* method 正在被调用的方法
* args 调用方法时,传入的参数
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("The method" + method.getName()+"begin with" +Arrays.asList(args));
Object result = method.invoke(target, args);
System.out.println("The method" + method.getName()+"end with "+result);
return result;
}
};
proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h);
return proxy;
}
}
测试
import com.test.dasenlin.generate.aop.ArithmeticCalculator;
import com.test.dasenlin.generate.aop.ArithmeticCalculatorImpl;
import com.test.dasenlin.generate.aop.ArithmeticCalculatorLoggingCglibProxy;
public class Main2 {
/**
* @param args
*/
public static void main(String[] args) {
ArithmeticCalculator target = new ArithmeticCalculatorImpl();
ArithmeticCalculator proxy = new ArithmeticCalculatorLoggingCglibProxy(target).getLoggingProxy();
int result = proxy.getPlus(1, 3);
System.out.println("--->"+result);
result = proxy.getMultiply(1, 5);
System.out.println("--->"+result);
}
}
结果
The methodgetPlusbegin with[1, 3]
The methodgetPlusend with 4
The methodgetMultiplybegin with[1, 5]
The methodgetMultiplyend with 5
aop相关术语