mirror of
https://github.com/Estom/notes.git
synced 2026-05-04 13:20:57 +08:00
spring 完结
This commit is contained in:
9
Spring/Spring5/code/shangguigu05/src/Main.java
Normal file
9
Spring/Spring5/code/shangguigu05/src/Main.java
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version $Id: ${NAME}, v 0.1 ${YEAR}-${MONTH}-${DAY} ${TIME} yinkanglong Exp $
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello world!");
|
||||
}
|
||||
}
|
||||
14
Spring/Spring5/code/shangguigu05/src/bean01.xml
Normal file
14
Spring/Spring5/code/shangguigu05/src/bean01.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<!--开启组件扫描
|
||||
1. 如果扫描多个包,多个包之间使用逗号隔开
|
||||
2. 扫描包的上层目录-->
|
||||
<context:component-scan base-package="com.ykl"></context:component-scan>
|
||||
|
||||
</beans>
|
||||
|
||||
17
Spring/Spring5/code/shangguigu05/src/bean02.xml
Normal file
17
Spring/Spring5/code/shangguigu05/src/bean02.xml
Normal file
@@ -0,0 +1,17 @@
|
||||
<?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"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
|
||||
|
||||
<!--开启组件扫描
|
||||
1. 如果扫描多个包,多个包之间使用逗号隔开
|
||||
2. 扫描包的上层目录-->
|
||||
<context:component-scan base-package="com.ykl"></context:component-scan>
|
||||
<!--开启aspectJ生成代理对象-->
|
||||
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
|
||||
</beans>
|
||||
|
||||
25
Spring/Spring5/code/shangguigu05/src/bean03.xml
Normal file
25
Spring/Spring5/code/shangguigu05/src/bean03.xml
Normal file
@@ -0,0 +1,25 @@
|
||||
<?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"
|
||||
xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
|
||||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
|
||||
|
||||
|
||||
<!--创建对象-->
|
||||
<bean id="book" class="com.ykl.aopxml.Book"></bean>
|
||||
<bean id="bookProxy" class="com.ykl.aopxml.BookProxy"></bean>
|
||||
|
||||
<aop:config>
|
||||
<!--切入点配置-->
|
||||
<aop:pointcut id="p" expression="execution(* com.ykl.aopxml.Book.add(..))"></aop:pointcut>
|
||||
<!--切面配置-->
|
||||
<aop:aspect ref="bookProxy">
|
||||
<aop:before method="before" pointcut-ref="p"></aop:before>
|
||||
</aop:aspect>
|
||||
</aop:config>
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.aopanno;
|
||||
|
||||
import org.aspectj.lang.annotation.After;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : PersonProxy, v 0.1 2022-10-09 16:01 yinkanglong Exp $
|
||||
*/
|
||||
@Component
|
||||
@Aspect
|
||||
@Order(1)
|
||||
public class PersonProxy {
|
||||
|
||||
|
||||
//前置通知
|
||||
@Before(value = "execution(* com.ykl.aopanno.User.add(..))")
|
||||
public void before(){
|
||||
System.out.println("执行前...");
|
||||
}
|
||||
|
||||
@After(value = "execution(* com.ykl.aopanno.User.add(..))")
|
||||
public void after(){
|
||||
System.out.println("after...");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.aopanno;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : User, v 0.1 2022-10-09 15:16 yinkanglong Exp $
|
||||
*/
|
||||
@Component
|
||||
public class User {
|
||||
public void add(){
|
||||
System.out.println("User add ...");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.aopanno;
|
||||
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : UserProxy, v 0.1 2022-10-09 15:16 yinkanglong Exp $
|
||||
*/
|
||||
@Component
|
||||
@Aspect
|
||||
public class UserProxy {
|
||||
|
||||
//前置通知
|
||||
@Before(value = "execution(* com.ykl.aopanno.User.add(..))")
|
||||
public void before(){
|
||||
System.out.println("执行前...");
|
||||
}
|
||||
|
||||
@After(value = "execution(* com.ykl.aopanno.User.add(..))")
|
||||
public void after(){
|
||||
System.out.println("after...");
|
||||
}
|
||||
|
||||
@AfterReturning(value = "execution(* com.ykl.aopanno.User.add(..))")
|
||||
public void afterReturn(){
|
||||
System.out.println("afterReturn...");
|
||||
}
|
||||
|
||||
@AfterThrowing(value = "execution(* com.ykl.aopanno.User.add(..))")
|
||||
public void afterThrow(){
|
||||
System.out.println("afterThrow...");
|
||||
}
|
||||
|
||||
@Around(value = "execution(* com.ykl.aopanno.User.add(..))")
|
||||
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
|
||||
System.out.println("环绕之前....");
|
||||
|
||||
proceedingJoinPoint.proceed();
|
||||
|
||||
System.out.println("环绕之后....");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.aopxml;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : Book, v 0.1 2022-10-09 16:06 yinkanglong Exp $
|
||||
*/
|
||||
public class Book {
|
||||
public void add(){
|
||||
System.out.println("Book add ...");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.aopxml;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : BookProxy, v 0.1 2022-10-09 16:06 yinkanglong Exp $
|
||||
*/
|
||||
public class BookProxy {
|
||||
public void before(){
|
||||
System.out.println("before ......");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.dao;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : JDKProxy, v 0.1 2022-10-09 14:31 yinkanglong Exp $
|
||||
*/
|
||||
public class JDKProxy {
|
||||
public static void main(String[] args) {
|
||||
Class[] interfaces = {UserDo.class};
|
||||
|
||||
// Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new InvocationHandler() {
|
||||
// @Override
|
||||
// public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
// return null;
|
||||
// }
|
||||
// });
|
||||
|
||||
UserDoImpl userDo = new UserDoImpl();
|
||||
UserDo dao = (UserDo)Proxy.newProxyInstance(JDKProxy.class.getClassLoader(),interfaces,new UserDaoProxy(userDo));
|
||||
int result = dao.add(1,2);
|
||||
System.out.println("结束");
|
||||
}
|
||||
}
|
||||
class UserDaoProxy implements InvocationHandler{
|
||||
Object object;
|
||||
//把被代理的对象,传递进来。通过有参构造进行传递
|
||||
public UserDaoProxy(Object object){
|
||||
this.object=object;
|
||||
}
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
||||
System.out.println("方法之前的执行"+method.getName());
|
||||
|
||||
Object res = method.invoke(object,args);
|
||||
|
||||
System.out.println("方法执行后");
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
14
Spring/Spring5/code/shangguigu05/src/com/ykl/dao/UserDo.java
Normal file
14
Spring/Spring5/code/shangguigu05/src/com/ykl/dao/UserDo.java
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.dao;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : UserDo, v 0.1 2022-10-09 12:03 yinkanglong Exp $
|
||||
*/
|
||||
public interface UserDo {
|
||||
public int add(int a,int b);
|
||||
public String update(String id);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.dao;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : UserDoImpl, v 0.1 2022-10-09 12:04 yinkanglong Exp $
|
||||
*/
|
||||
@Repository
|
||||
public class UserDoImpl implements UserDo{
|
||||
|
||||
@Override
|
||||
public int add(int a, int b) {
|
||||
System.out.println("add 方法执行了");
|
||||
return a+b;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String update(String id) {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.test;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : Test01, v 0.1 2022-10-08 16:09 yinkanglong Exp $
|
||||
*/
|
||||
public class Test01 {
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Alipay.com Inc.
|
||||
* Copyright (c) 2004-2022 All Rights Reserved.
|
||||
*/
|
||||
package com.ykl.test;
|
||||
|
||||
import com.ykl.aopanno.User;
|
||||
import com.ykl.aopxml.Book;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author yinkanglong
|
||||
* @version : Test02, v 0.1 2022-10-09 15:28 yinkanglong Exp $
|
||||
*/
|
||||
public class Test02 {
|
||||
|
||||
@Test
|
||||
public void testAop(){
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("bean02.xml");
|
||||
User user = context.getBean("user", User.class);
|
||||
user.add();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAopXml(){
|
||||
ApplicationContext context = new ClassPathXmlApplicationContext("bean03.xml");
|
||||
Book book = context.getBean("book", Book.class);
|
||||
book.add();
|
||||
}
|
||||
}
|
||||
4
Spring/Spring5/code/shangguigu05/src/jdbc.properties
Normal file
4
Spring/Spring5/code/shangguigu05/src/jdbc.properties
Normal file
@@ -0,0 +1,4 @@
|
||||
prop.driverClass=com.mysql.jdbc.Driver
|
||||
prop.url=jdbc:mysql://localhost:3306/user
|
||||
prop.username=root
|
||||
prop.password=123456
|
||||
Reference in New Issue
Block a user