Skip to content

Commit 07aafc5

Browse files
committed
Rainbow aop review and test.
1 parent 14b5e44 commit 07aafc5

File tree

17 files changed

+805
-395
lines changed

17 files changed

+805
-395
lines changed

.idea/workspace.xml

Lines changed: 348 additions & 395 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rainbow.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
2424
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
2525
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
26+
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
2627
</content>
2728
<orderEntry type="inheritedJdk" />
2829
<orderEntry type="sourceFolder" forTests="false" />
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package rainbow;
2+
3+
import Utility.RedisCache;
4+
import dao.UserDao;
5+
import dao.UserDaoImpl;
6+
import domain.UserInfo;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.beans.factory.annotation.Qualifier;
9+
import org.springframework.beans.factory.annotation.Required;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Scope;
12+
import org.springframework.stereotype.Component;
13+
import org.springframework.stereotype.Controller;
14+
import org.springframework.stereotype.Repository;
15+
import org.springframework.stereotype.Service;
16+
import org.springframework.web.bind.annotation.InitBinder;
17+
import service.UserService;
18+
import service.UserServiceImpl;
19+
20+
import javax.annotation.PostConstruct;
21+
import javax.annotation.PreDestroy;
22+
import javax.annotation.Resource;
23+
24+
/**
25+
* Created by pengfei on 2017/9/13.
26+
*/
27+
@Repository
28+
@Service
29+
@Component("annotation")
30+
@Controller
31+
@Resource(name="annotationTool")
32+
public class Annotation1 {
33+
34+
35+
@Autowired(required = false)
36+
private int age;
37+
38+
@Resource(name="redisCache2")
39+
private RedisCache cache;
40+
41+
@Required
42+
public void setAge(int age) {
43+
this.age = age;
44+
}
45+
46+
@Autowired(required = false)
47+
private UserService service;
48+
49+
@Autowired
50+
@Qualifier("userDAO2")
51+
public UserDao getUserDao(){
52+
return new UserDaoImpl();
53+
}
54+
55+
@Autowired
56+
@Resource(type = UserServiceImpl.class)
57+
public void testUserService(UserService userService){
58+
System.out.println("Auto wired user service");
59+
}
60+
61+
@Bean
62+
@Scope("singleton")
63+
public UserInfo test(){
64+
return new UserInfo();
65+
}
66+
67+
@PostConstruct
68+
public void preInit(){
69+
70+
}
71+
72+
73+
@PreDestroy
74+
public void preDestroy(){
75+
76+
}
77+
78+
79+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package rainbow;
2+
3+
import dao.UserDao;
4+
import dao.UserDaoImpl;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Import;
8+
import org.springframework.context.annotation.Scope;
9+
10+
/**
11+
* Created by pengfei on 2017/9/14.
12+
*/
13+
14+
@Configuration
15+
@Import({Annotation1.class,MapLearn.class,JedisTest.class})
16+
public class AnnotationB {
17+
18+
@Bean(initMethod = "")
19+
@Scope("singleton")
20+
public UserDao userDao() {
21+
return new UserDaoImpl();
22+
}
23+
24+
25+
public void test(){
26+
27+
}
28+
}

src/test/java/rainbow/MapLearn.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package rainbow;
22

3+
import org.springframework.context.annotation.Configuration;
4+
35
import java.util.HashMap;
46
import java.util.Map;
57

68
/**
79
* Created by pengfei on 2017/8/31.
810
*/
11+
@Configuration
912
public class MapLearn {
1013
public static void main(String[] args) {
1114
Map<String, String> map = new HashMap<String, String>();

src/test/java/rainbow/TestJunit.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package rainbow;
2+
3+
import org.junit.*;
4+
import org.junit.rules.ExpectedException;
5+
6+
/**
7+
* Created by pengfei on 2017/9/13.
8+
*/
9+
public class TestJunit {
10+
11+
@BeforeClass
12+
public static void testBeforeClass() {
13+
System.out.println("before class");
14+
}
15+
16+
@Before
17+
public void init() {
18+
System.out.println("init here");
19+
}
20+
21+
@Test
22+
public void testAandB() {
23+
int a = 3;
24+
int b = 4;
25+
Assert.assertEquals(7, (a + b));
26+
}
27+
28+
@Test(expected=AssertionError.class)
29+
public void testAsubB() {
30+
int a = 3;
31+
assert a > 5;
32+
int b = 5;
33+
Assert.assertEquals(-1, (a - b));
34+
}
35+
36+
@Ignore
37+
public void testIgnore() {
38+
System.out.println("ignore");
39+
}
40+
41+
@After
42+
public void after() {
43+
System.out.println("after");
44+
}
45+
46+
@AfterClass
47+
public static void afterClass() {
48+
System.out.println("after class");
49+
}
50+
}

src/test/java/rainbow/TestSM.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@
22

33
import Utility.RedisCache;
44
import domain.UserInfo;
5+
import org.springframework.beans.factory.BeanFactory;
6+
import org.springframework.beans.factory.xml.XmlBeanFactory;
57
import org.springframework.context.ApplicationContext;
8+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
9+
import org.springframework.context.support.AbstractApplicationContext;
610
import org.springframework.context.support.ClassPathXmlApplicationContext;
11+
import org.springframework.context.support.FileSystemXmlApplicationContext;
12+
import org.springframework.core.io.ClassPathResource;
13+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
14+
import org.springframework.web.context.support.XmlWebApplicationContext;
715
import service.IUserService;
16+
import service.UserService;
817

918
/**
1019
* Created by pengfei on 2017/9/2.
@@ -14,8 +23,19 @@ public class TestSM {
1423
public static void main(String[] args) {
1524
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:mybatis/application.xml");
1625

26+
context=new FileSystemXmlApplicationContext();
27+
context=new AnnotationConfigApplicationContext();
28+
context=new AnnotationConfigWebApplicationContext();
29+
context=new XmlWebApplicationContext();
30+
31+
BeanFactory factory=new XmlBeanFactory(new ClassPathResource("mybatis/application.xml"));
32+
UserService service= (UserService) factory.getBean("userService");
33+
34+
1735
summaryTest(context);
1836

37+
((AbstractApplicationContext)context).registerShutdownHook();
38+
1939
System.exit(-1);
2040
}
2141

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package rainbow.aop.afterAdvice;
2+
3+
4+
import org.springframework.aop.AfterReturningAdvice;
5+
import org.springframework.aop.framework.ProxyFactory;
6+
7+
import java.lang.reflect.Method;
8+
9+
/**
10+
* Created by pengfei on 2017/9/15.
11+
*/
12+
public class AfterAdvice {
13+
public static void main(String[] args) {
14+
MessageInfo target=new MessageInfo();
15+
MessageAfterAdvice advice=new MessageAfterAdvice();
16+
ProxyFactory factory=new ProxyFactory();
17+
18+
factory.setTarget(target);
19+
factory.addAdvice(advice);
20+
MessageInfo proxyObj= (MessageInfo) factory.getProxy();
21+
22+
proxyObj.info();
23+
24+
}
25+
}
26+
27+
class MessageAfterAdvice implements AfterReturningAdvice {
28+
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
29+
System.out.println("after advice");
30+
}
31+
}
32+
33+
class MessageInfo {
34+
public void info(){
35+
System.out.println("Message info");
36+
}
37+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package rainbow.aop.aroundAdvice;
2+
3+
4+
import org.aopalliance.intercept.MethodInterceptor;
5+
import org.aopalliance.intercept.MethodInvocation;
6+
import org.springframework.aop.framework.ProxyFactory;
7+
8+
/**
9+
* Created by pengfei on 2017/9/15.
10+
*/
11+
public class AroundAdvice {
12+
13+
public static void main(String[] args) {
14+
MessageInfo target = new MessageInfo();
15+
MessageInfoAdvice advice = new MessageInfoAdvice();
16+
ProxyFactory factory = new ProxyFactory();
17+
18+
factory.setTarget(target);
19+
factory.addAdvice(advice);
20+
MessageInfo proxyObj = (MessageInfo) factory.getProxy();
21+
22+
proxyObj.info();
23+
24+
25+
}
26+
}
27+
28+
class MessageInfoAdvice implements MethodInterceptor {
29+
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
30+
System.out.println("before method");
31+
Object obj = methodInvocation.proceed();
32+
System.out.println("after method");
33+
34+
return obj;
35+
}
36+
}
37+
38+
39+
class MessageInfo {
40+
public void info() {
41+
System.out.println("message info");
42+
}
43+
}
44+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package rainbow.aop.beforeAdvice;
2+
3+
import org.springframework.aop.MethodBeforeAdvice;
4+
import org.springframework.aop.framework.ProxyFactory;
5+
6+
import java.lang.reflect.Method;
7+
8+
/**
9+
* Created by pengfei on 2017/9/15.
10+
*/
11+
public class Message {
12+
13+
public static void main(String[] args) {
14+
MessageInfo target=new MessageInfo();
15+
MessageAdvice advice=new MessageAdvice();
16+
ProxyFactory factory=new ProxyFactory();
17+
18+
factory.setTarget(target);
19+
factory.addAdvice(advice);
20+
21+
MessageInfo proxyObj= (MessageInfo) factory.getProxy();
22+
23+
proxyObj.message();
24+
25+
}
26+
}
27+
28+
class MessageAdvice implements MethodBeforeAdvice {
29+
30+
public void before(Method method, Object[] objects, Object o) throws Throwable {
31+
System.out.println("Before method:"+method.getName());
32+
}
33+
}
34+
35+
class MessageInfo{
36+
public void message(){
37+
System.out.println("Message info:");
38+
}
39+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package rainbow.aop.cglibProxy;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
import rainbow.aop.jdkProxy.Message;
6+
7+
/**
8+
* Created by pengfei on 2017/9/15.
9+
*/
10+
public class CGlibProxyTest {
11+
public static void main(String[] args) {
12+
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:cglibProxy.xml");
13+
14+
Message proxyObj= (Message) context.getBean("cglibProxyFactoryBean");
15+
proxyObj.info();
16+
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package rainbow.aop.jdkProxy;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.support.ClassPathXmlApplicationContext;
5+
6+
/**
7+
* Created by pengfei on 2017/9/15.
8+
*/
9+
public class JDKProxyTest {
10+
11+
public static void main(String[] args) {
12+
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:jdkProxy.xml");
13+
14+
Message proxyObj= (Message) context.getBean("jdkProxyFactoryBean");
15+
proxyObj.info();
16+
17+
}
18+
}

0 commit comments

Comments
 (0)