Browse Source

自定义mybatis

zhang 1 year ago
parent
commit
06b3cc1f2f
49 changed files with 508 additions and 12 deletions
  1. 5 0
      mybatis_train/pom.xml
  2. 10 0
      mybatis_train/src/main/java/MybatisTest.java
  3. 0 1
      mybatis_train/src/main/java/com/mapper/AccountMapper.java
  4. 4 0
      mybatis_train/src/main/java/com/service/impl/AccountServiceImpl.java
  5. 17 0
      mybatis_train/src/main/java/com/zhouyuTest/Test.java
  6. 24 0
      mybatis_train/src/main/java/com/zhouyuTest/config/MyConfig.java
  7. 8 0
      mybatis_train/src/main/java/com/zhouyuTest/mapper/OrderMapper.java
  8. 10 0
      mybatis_train/src/main/java/com/zhouyuTest/mapper/UserMapper.java
  9. 18 0
      mybatis_train/src/main/java/com/zhouyuTest/service/UserService.java
  10. 24 0
      mybatis_train/src/main/java/com/zhouyuTest/spring_mybatis/MyImportBeanDefinitionRegistrar.java
  11. 46 0
      mybatis_train/src/main/java/com/zhouyuTest/spring_mybatis/MybatisFactoryBean.java
  12. 1 0
      pom.xml
  13. 37 0
      shiro_login/pom.xml
  14. 4 0
      shiro_login/src/main/java/com/bean/UserBean.java
  15. 5 0
      shiro_login/src/main/java/com/service/UserService.java
  16. 0 0
      shiro_login/src/main/resources/shiro.ini
  17. 5 0
      shiro_login/src/test/java/Test.java
  18. 6 0
      shiro_login/web/WEB-INF/web.xml
  19. 16 0
      shiro_login/web/index.jsp
  20. 5 2
      springMvc/src/main/java/com/controller/UserController.java
  21. 12 0
      springMvc/src/main/java/com/form/UserForm.java
  22. BIN
      springMvc/src/main/webapp/js/test.jpg
  23. 4 0
      spring_annotation/src/main/java/AnnotationMain.java
  24. 10 0
      spring_annotation/src/main/java/BeanConstructTest.java
  25. 2 0
      spring_annotation/src/main/java/com/Config/SpringConfiguration.java
  26. 6 0
      spring_annotation/src/main/java/com/Config/TestConfigProperties.java
  27. 2 2
      spring_annotation/src/main/java/com/Config/TestConfiguration.java
  28. 17 1
      spring_annotation/src/main/java/com/service/impl/JwtServiceImpl.java
  29. 2 2
      spring_annotation/src/main/resources/jdbc.properties
  30. 2 0
      spring_aop/src/main/java/com/AopTest.java
  31. 17 0
      spring_aop/src/main/java/com/CricleDependent/AService.java
  32. 13 0
      spring_aop/src/main/java/com/CricleDependent/BService.java
  33. 14 0
      spring_aop/src/main/java/com/CricleDependent/Config.java
  34. 22 0
      spring_aop/src/main/java/com/CricleDependent/DependentAspect.java
  35. 13 0
      spring_aop/src/main/java/com/CricleDependent/Main.java
  36. 14 0
      spring_aop/src/main/java/com/FactoryBeanTest.java
  37. 13 0
      spring_aop/src/main/java/com/SpringTest.java
  38. 5 0
      spring_aop/src/main/java/com/TestTx.java
  39. 11 1
      spring_aop/src/main/java/com/anno/TargetImpl.java
  40. 7 0
      spring_aop/src/main/java/com/anno/service/MyService.java
  41. 21 0
      spring_aop/src/main/java/com/config/MyBeanProcessor.java
  42. 12 0
      spring_aop/src/main/java/com/config/NameConfig.java
  43. 24 0
      spring_aop/src/main/java/com/factory_bean/MyFactoryBean.java
  44. 8 0
      spring_aop/src/main/java/com/factory_bean/MyUser.java
  45. 2 1
      spring_aop/src/main/java/com/transaction_anno/service/impl/AccountServiceImpl.java
  46. 2 1
      spring_aop/src/main/java/com/transaction_anno/service/impl/AccountTwoServiceImpl.java
  47. 5 1
      spring_aop/src/main/java/com/transaction_anno/service/impl/AccountTxServiceImpl.java
  48. 1 0
      spring_aop/src/main/resources/context.xml
  49. 2 0
      spring_aop/src/main/resources/self_design.properties

+ 5 - 0
mybatis_train/pom.xml

@@ -62,6 +62,11 @@
62
             <artifactId>pagehelper</artifactId>
62
             <artifactId>pagehelper</artifactId>
63
             <version>5.2.0</version>
63
             <version>5.2.0</version>
64
         </dependency>
64
         </dependency>
65
+        <dependency>
66
+            <groupId>org.springframework</groupId>
67
+            <artifactId>spring-jdbc</artifactId>
68
+            <version>5.3.19</version>
69
+        </dependency>
65
     </dependencies>
70
     </dependencies>
66
 
71
 
67
 </project>
72
 </project>

+ 10 - 0
mybatis_train/src/main/java/MybatisTest.java

@@ -0,0 +1,10 @@
1
+import com.mapper.AccountMapper;
2
+import org.springframework.context.support.ClassPathXmlApplicationContext;
3
+
4
+public class MybatisTest {
5
+    public static void main(String[] args) {
6
+        ClassPathXmlApplicationContext classPathXmlApplicationContext = new
7
+                ClassPathXmlApplicationContext("classpath:mybatisConfig.xml");
8
+        AccountMapper bean = classPathXmlApplicationContext.getBean(AccountMapper.class);
9
+    }
10
+}

+ 0 - 1
mybatis_train/src/main/java/com/mapper/AccountMapper.java

@@ -10,7 +10,6 @@ import org.springframework.stereotype.Repository;
10
 
10
 
11
 import java.util.List;
11
 import java.util.List;
12
 import java.util.Map;
12
 import java.util.Map;
13
-
14
 public interface AccountMapper {
13
 public interface AccountMapper {
15
     List<AccountEntity> findAll(Integer id);
14
     List<AccountEntity> findAll(Integer id);
16
     void updateOne(AccountEntity accountEntity);
15
     void updateOne(AccountEntity accountEntity);

+ 4 - 0
mybatis_train/src/main/java/com/service/impl/AccountServiceImpl.java

@@ -1,9 +1,13 @@
1
 package com.service.impl;
1
 package com.service.impl;
2
 
2
 
3
+import com.mapper.AccountMapper;
4
+import org.springframework.beans.factory.annotation.Autowired;
3
 import org.springframework.stereotype.Service;
5
 import org.springframework.stereotype.Service;
4
 
6
 
5
 @Service
7
 @Service
6
 public class AccountServiceImpl {
8
 public class AccountServiceImpl {
9
+    @Autowired
10
+    AccountMapper accountMapper;
7
     public void sayHello() {
11
     public void sayHello() {
8
         System.out.println("say");
12
         System.out.println("say");
9
     }
13
     }

+ 17 - 0
mybatis_train/src/main/java/com/zhouyuTest/Test.java

@@ -0,0 +1,17 @@
1
+package com.zhouyuTest;
2
+
3
+import com.zhouyuTest.config.MyConfig;
4
+import com.zhouyuTest.mapper.UserMapper;
5
+import com.zhouyuTest.service.UserService;
6
+import com.zhouyuTest.spring_mybatis.MybatisFactoryBean;
7
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
8
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
9
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
10
+
11
+public class Test {
12
+    public static void main(String[] args) {
13
+        AnnotationConfigApplicationContext an = new AnnotationConfigApplicationContext(MyConfig.class);
14
+        UserService bean = an.getBean(UserService.class);
15
+        bean.test();
16
+    }
17
+}

+ 24 - 0
mybatis_train/src/main/java/com/zhouyuTest/config/MyConfig.java

@@ -0,0 +1,24 @@
1
+package com.zhouyuTest.config;
2
+
3
+import com.zhouyuTest.spring_mybatis.MyImportBeanDefinitionRegistrar;
4
+import org.apache.ibatis.io.Resources;
5
+import org.apache.ibatis.session.SqlSessionFactory;
6
+import org.apache.ibatis.session.SqlSessionFactoryBuilder;
7
+import org.springframework.context.annotation.Bean;
8
+import org.springframework.context.annotation.ComponentScan;
9
+import org.springframework.context.annotation.Configuration;
10
+import org.springframework.context.annotation.Import;
11
+
12
+import java.io.IOException;
13
+import java.io.InputStream;
14
+
15
+@Configuration
16
+@ComponentScan({"com.zhouyuTest"})
17
+@Import(MyImportBeanDefinitionRegistrar.class)
18
+public class MyConfig {
19
+    @Bean
20
+    public SqlSessionFactory getFactory() throws IOException {
21
+        InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
22
+        return new SqlSessionFactoryBuilder().build(resourceAsStream);
23
+    }
24
+}

+ 8 - 0
mybatis_train/src/main/java/com/zhouyuTest/mapper/OrderMapper.java

@@ -0,0 +1,8 @@
1
+package com.zhouyuTest.mapper;
2
+
3
+import org.apache.ibatis.annotations.Select;
4
+
5
+public interface OrderMapper {
6
+    @Select("select 'order'")
7
+    public String getOrder();
8
+}

+ 10 - 0
mybatis_train/src/main/java/com/zhouyuTest/mapper/UserMapper.java

@@ -0,0 +1,10 @@
1
+package com.zhouyuTest.mapper;
2
+
3
+import org.apache.ibatis.annotations.Mapper;
4
+import org.apache.ibatis.annotations.Select;
5
+
6
+public interface UserMapper {
7
+    @Select("select 'user1'")
8
+    public String getUser();
9
+
10
+}

+ 18 - 0
mybatis_train/src/main/java/com/zhouyuTest/service/UserService.java

@@ -0,0 +1,18 @@
1
+package com.zhouyuTest.service;
2
+
3
+import com.zhouyuTest.mapper.OrderMapper;
4
+import com.zhouyuTest.mapper.UserMapper;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.stereotype.Component;
7
+
8
+@Component
9
+public class UserService {
10
+    @Autowired
11
+    UserMapper userMapper;
12
+    @Autowired
13
+    OrderMapper orderMapper;
14
+    public void test() {
15
+        System.out.println(userMapper.getUser());
16
+        System.out.println(orderMapper.getOrder());
17
+    }
18
+}

+ 24 - 0
mybatis_train/src/main/java/com/zhouyuTest/spring_mybatis/MyImportBeanDefinitionRegistrar.java

@@ -0,0 +1,24 @@
1
+package com.zhouyuTest.spring_mybatis;
2
+
3
+import com.zhouyuTest.mapper.OrderMapper;
4
+import com.zhouyuTest.mapper.UserMapper;
5
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
6
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
7
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
8
+import org.springframework.beans.factory.support.BeanNameGenerator;
9
+import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
10
+import org.springframework.core.type.AnnotationMetadata;
11
+
12
+public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
13
+    @Override
14
+    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
15
+        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
16
+        beanDefinition.setBeanClass(MybatisFactoryBean.class);
17
+        beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(UserMapper.class);
18
+        registry.registerBeanDefinition("userMapper",beanDefinition);
19
+        AbstractBeanDefinition beanDefinition2 = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition();
20
+        beanDefinition2.setBeanClass(MybatisFactoryBean.class);
21
+        beanDefinition2.getConstructorArgumentValues().addGenericArgumentValue(OrderMapper.class);
22
+        registry.registerBeanDefinition("orderMapper",beanDefinition2);
23
+    }
24
+}

+ 46 - 0
mybatis_train/src/main/java/com/zhouyuTest/spring_mybatis/MybatisFactoryBean.java

@@ -0,0 +1,46 @@
1
+package com.zhouyuTest.spring_mybatis;
2
+
3
+import com.zhouyuTest.mapper.UserMapper;
4
+import org.apache.ibatis.session.SqlSession;
5
+import org.apache.ibatis.session.SqlSessionFactory;
6
+import org.springframework.beans.factory.FactoryBean;
7
+import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.stereotype.Component;
9
+
10
+import java.lang.reflect.InvocationHandler;
11
+import java.lang.reflect.Method;
12
+import java.lang.reflect.Proxy;
13
+
14
+//@Component
15
+public class MybatisFactoryBean implements FactoryBean {
16
+    private SqlSession sqlSession;
17
+    private Class mapperClass;
18
+
19
+    public MybatisFactoryBean(Class mapperClass) {
20
+        this.mapperClass = mapperClass;
21
+    }
22
+
23
+    @Autowired
24
+    public void setSqlSession(SqlSessionFactory sessionFactory) {
25
+        sessionFactory.getConfiguration().addMapper(mapperClass);
26
+        this.sqlSession = sessionFactory.openSession(true);
27
+    }
28
+
29
+    @Override
30
+    public Object getObject() throws Exception {
31
+//        Object o = Proxy.newProxyInstance(MybatisFactoryBean.class.getClassLoader(), new Class[]{UserMapper.class}, new InvocationHandler() {
32
+//            @Override
33
+//            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
34
+//                System.out.println(method.getName());
35
+//                return null;
36
+//            }
37
+//        });
38
+//
39
+//        return o;
40
+        return sqlSession.getMapper(mapperClass);
41
+    }
42
+    @Override
43
+    public Class<?> getObjectType() {
44
+        return mapperClass;
45
+    }
46
+}

+ 1 - 0
pom.xml

@@ -14,6 +14,7 @@
14
         <module>spring_test</module>
14
         <module>spring_test</module>
15
         <module>spring_aop</module>
15
         <module>spring_aop</module>
16
         <module>mybatis_train</module>
16
         <module>mybatis_train</module>
17
+        <module>shiro_login</module>
17
     </modules>
18
     </modules>
18
 
19
 
19
     <properties>
20
     <properties>

+ 37 - 0
shiro_login/pom.xml

@@ -0,0 +1,37 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0"
3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <parent>
6
+        <artifactId>spring_demo</artifactId>
7
+        <groupId>org.example</groupId>
8
+        <version>1.0-SNAPSHOT</version>
9
+    </parent>
10
+    <modelVersion>4.0.0</modelVersion>
11
+
12
+    <artifactId>shiro_login</artifactId>
13
+    <dependencies>
14
+        <dependency>
15
+            <groupId>org.apache.shiro</groupId>
16
+            <artifactId>shiro-core</artifactId>
17
+            <version>1.7.0</version>
18
+        </dependency>
19
+        <dependency>
20
+            <groupId>org.springframework</groupId>
21
+            <artifactId>spring-webmvc</artifactId>
22
+            <version>5.3.6</version>
23
+        </dependency>
24
+        <dependency>
25
+            <groupId>commons-logging</groupId>
26
+            <artifactId>commons-logging</artifactId>
27
+            <version>1.2</version>
28
+        </dependency>
29
+    </dependencies>
30
+
31
+
32
+    <properties>
33
+        <maven.compiler.source>8</maven.compiler.source>
34
+        <maven.compiler.target>8</maven.compiler.target>
35
+    </properties>
36
+
37
+</project>

+ 4 - 0
shiro_login/src/main/java/com/bean/UserBean.java

@@ -0,0 +1,4 @@
1
+package com.bean;
2
+
3
+public class UserBean {
4
+}

+ 5 - 0
shiro_login/src/main/java/com/service/UserService.java

@@ -0,0 +1,5 @@
1
+package com.service;
2
+
3
+
4
+public class UserService {
5
+}

+ 0 - 0
shiro_login/src/main/resources/shiro.ini


+ 5 - 0
shiro_login/src/test/java/Test.java

@@ -0,0 +1,5 @@
1
+public class Test {
2
+    public static void main(String[] args) {
3
+
4
+    }
5
+}

+ 6 - 0
shiro_login/web/WEB-INF/web.xml

@@ -0,0 +1,6 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
5
+         version="4.0">
6
+</web-app>

+ 16 - 0
shiro_login/web/index.jsp

@@ -0,0 +1,16 @@
1
+<%--
2
+  Created by IntelliJ IDEA.
3
+  User: 77179
4
+  Date: 2022/10/9
5
+  Time: 22:00
6
+  To change this template use File | Settings | File Templates.
7
+--%>
8
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
9
+<html>
10
+  <head>
11
+    <title>$Title$</title>
12
+  </head>
13
+  <body>
14
+  $END$
15
+  </body>
16
+</html>

+ 5 - 2
springMvc/src/main/java/com/controller/UserController.java

@@ -1,8 +1,11 @@
1
 package com.controller;
1
 package com.controller;
2
 
2
 
3
+import com.form.UserForm;
3
 import com.transaction_.pojo.entity.Student;
4
 import com.transaction_.pojo.entity.Student;
4
 import org.springframework.beans.factory.annotation.Autowired;
5
 import org.springframework.beans.factory.annotation.Autowired;
5
 import org.springframework.stereotype.Controller;
6
 import org.springframework.stereotype.Controller;
7
+import org.springframework.web.bind.annotation.PostMapping;
8
+import org.springframework.web.bind.annotation.RequestBody;
6
 import org.springframework.web.bind.annotation.RequestMapping;
9
 import org.springframework.web.bind.annotation.RequestMapping;
7
 import org.springframework.web.bind.annotation.ResponseBody;
10
 import org.springframework.web.bind.annotation.ResponseBody;
8
 import org.springframework.web.servlet.ModelAndView;
11
 import org.springframework.web.servlet.ModelAndView;
@@ -42,9 +45,9 @@ public class UserController {
42
         return "save2";
45
         return "save2";
43
     }
46
     }
44
     // 回写数据 返回对象
47
     // 回写数据 返回对象
45
-    @RequestMapping("/save3")
48
+    @PostMapping(value = "/save3")
46
     @ResponseBody // 不进行视图跳转,直接进行数据响应
49
     @ResponseBody // 不进行视图跳转,直接进行数据响应
47
-    public Student save3() {
50
+    public Student save3(@RequestBody UserForm userForm) {
48
         Student student = new Student();
51
         Student student = new Student();
49
         student.setName("woshinishiws");
52
         student.setName("woshinishiws");
50
         return student;
53
         return student;

+ 12 - 0
springMvc/src/main/java/com/form/UserForm.java

@@ -0,0 +1,12 @@
1
+package com.form;
2
+
3
+import com.fasterxml.jackson.annotation.JsonProperty;
4
+import lombok.Data;
5
+
6
+@Data
7
+public class UserForm {
8
+    @JsonProperty("user_id")
9
+    private Integer userId;
10
+    @JsonProperty("user_name")
11
+    private String userName;
12
+}

BIN
springMvc/src/main/webapp/js/test.jpg


+ 4 - 0
spring_annotation/src/main/java/AnnotationMain.java

@@ -23,6 +23,10 @@ public class AnnotationMain {
23
 //        bean1.parseToken(s);
23
 //        bean1.parseToken(s);
24
 //        UserService userService = applicationContext.getBean(UserService.class);
24
 //        UserService userService = applicationContext.getBean(UserService.class);
25
 //        userService.addUserBatch();
25
 //        userService.addUserBatch();
26
+        AnnotationConfigApplicationContext testContext =
27
+                new AnnotationConfigApplicationContext(TestConfiguration.class);
28
+        String[] beanDefinitionNames = testContext.getBeanDefinitionNames();
29
+        User user12 = testContext.getBean("user1", User.class);
26
         TestConfiguration bean2 = applicationContext.getBean(TestConfiguration.class);
30
         TestConfiguration bean2 = applicationContext.getBean(TestConfiguration.class);
27
         User user1 = applicationContext.getBean("user1", User.class);
31
         User user1 = applicationContext.getBean("user1", User.class);
28
         User user11 = bean2.getUser1();
32
         User user11 = bean2.getUser1();

+ 10 - 0
spring_annotation/src/main/java/BeanConstructTest.java

@@ -0,0 +1,10 @@
1
+import com.Config.SpringConfiguration;
2
+import com.service.JwtService;
3
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4
+
5
+public class BeanConstructTest {
6
+    public static void main(String[] args) {
7
+        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);
8
+        JwtService bean = annotationConfigApplicationContext.getBean(JwtService.class);
9
+    }
10
+}

+ 2 - 0
spring_annotation/src/main/java/com/Config/SpringConfiguration.java

@@ -1,6 +1,8 @@
1
 package com.Config;
1
 package com.Config;
2
 
2
 
3
 import com.alibaba.druid.pool.DruidDataSource;
3
 import com.alibaba.druid.pool.DruidDataSource;
4
+import com.service.JwtService;
5
+import com.service.impl.JwtServiceImpl;
4
 import org.springframework.beans.factory.annotation.Value;
6
 import org.springframework.beans.factory.annotation.Value;
5
 import org.springframework.context.annotation.Bean;
7
 import org.springframework.context.annotation.Bean;
6
 import org.springframework.context.annotation.ComponentScan;
8
 import org.springframework.context.annotation.ComponentScan;

+ 6 - 0
spring_annotation/src/main/java/com/Config/TestConfigProperties.java

@@ -0,0 +1,6 @@
1
+package com.Config;
2
+
3
+public class TestConfigProperties {
4
+    private String name;
5
+    private String className;
6
+}

+ 2 - 2
spring_annotation/src/main/java/com/Config/TestConfiguration.java

@@ -4,8 +4,8 @@ import com.entity.User;
4
 import org.springframework.context.annotation.Bean;
4
 import org.springframework.context.annotation.Bean;
5
 import org.springframework.context.annotation.Configuration;
5
 import org.springframework.context.annotation.Configuration;
6
 
6
 
7
-@Configuration
8
-//@Configuration(proxyBeanMethods = false)
7
+//@Configuration
8
+@Configuration(proxyBeanMethods = true)
9
 public class TestConfiguration {
9
 public class TestConfiguration {
10
     @Bean
10
     @Bean
11
     public String testStringBean() {
11
     public String testStringBean() {

+ 17 - 1
spring_annotation/src/main/java/com/service/impl/JwtServiceImpl.java

@@ -7,6 +7,10 @@ import com.auth0.jwt.algorithms.Algorithm;
7
 import com.auth0.jwt.interfaces.Claim;
7
 import com.auth0.jwt.interfaces.Claim;
8
 import com.auth0.jwt.interfaces.DecodedJWT;
8
 import com.auth0.jwt.interfaces.DecodedJWT;
9
 import com.service.JwtService;
9
 import com.service.JwtService;
10
+import com.service.MqService;
11
+import com.service.StudentService;
12
+import org.springframework.beans.factory.InitializingBean;
13
+import org.springframework.beans.factory.annotation.Autowired;
10
 import org.springframework.stereotype.Service;
14
 import org.springframework.stereotype.Service;
11
 
15
 
12
 import java.util.Date;
16
 import java.util.Date;
@@ -14,8 +18,20 @@ import java.util.HashMap;
14
 import java.util.Map;
18
 import java.util.Map;
15
 
19
 
16
 @Service
20
 @Service
17
-public class JwtServiceImpl implements JwtService {
21
+public class JwtServiceImpl implements JwtService, InitializingBean {
18
     private static final String TOKEN_SECRET = "ZXY";
22
     private static final String TOKEN_SECRET = "ZXY";
23
+    @Autowired
24
+    JwtService service;
25
+
26
+    public JwtServiceImpl() {
27
+        System.out.println("111");
28
+    }
29
+
30
+    @Override
31
+    public void afterPropertiesSet() throws Exception {
32
+        System.out.println("属性设置之后执行");
33
+    }
34
+
19
     @Override
35
     @Override
20
     public String generateToken(String userId) {
36
     public String generateToken(String userId) {
21
         Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
37
         Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);

+ 2 - 2
spring_annotation/src/main/resources/jdbc.properties

@@ -1,4 +1,4 @@
1
 jdbc.driver=com.mysql.cj.jdbc.Driver
1
 jdbc.driver=com.mysql.cj.jdbc.Driver
2
-jdbc.url=jdbc:mysql://192.168.50.132:3306/test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
2
+jdbc.url=jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
3
 jdbc.username=root
3
 jdbc.username=root
4
-jdbc.password=123456
4
+jdbc.password=

+ 2 - 0
spring_aop/src/main/java/com/AopTest.java

@@ -31,5 +31,7 @@ public class AopTest {
31
     public void transform() {
31
     public void transform() {
32
         // 交易测试
32
         // 交易测试
33
         accountService.transfer(5,4,1.0);
33
         accountService.transfer(5,4,1.0);
34
+//        Class<?> aClass = Class.forName("com.");
35
+//        Object o = aClass.newInstance();
34
     }
36
     }
35
 }
37
 }

+ 17 - 0
spring_aop/src/main/java/com/CricleDependent/AService.java

@@ -0,0 +1,17 @@
1
+package com.CricleDependent;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.scheduling.annotation.Async;
5
+import org.springframework.stereotype.Service;
6
+
7
+@Service
8
+public class AService {
9
+    @Autowired
10
+    BService bService;
11
+
12
+//    @Async
13
+    public void test(){
14
+        System.out.println(bService.getClass());
15
+        System.out.println("test");
16
+    }
17
+}

+ 13 - 0
spring_aop/src/main/java/com/CricleDependent/BService.java

@@ -0,0 +1,13 @@
1
+package com.CricleDependent;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.stereotype.Service;
5
+
6
+@Service
7
+public class BService {
8
+    @Autowired
9
+    AService aService;
10
+    public void test() {
11
+        System.out.println(aService.getClass());
12
+    }
13
+}

+ 14 - 0
spring_aop/src/main/java/com/CricleDependent/Config.java

@@ -0,0 +1,14 @@
1
+package com.CricleDependent;
2
+
3
+import org.springframework.context.annotation.ComponentScan;
4
+import org.springframework.context.annotation.Configuration;
5
+import org.springframework.context.annotation.EnableAspectJAutoProxy;
6
+import org.springframework.scheduling.annotation.EnableAsync;
7
+
8
+@Configuration
9
+@ComponentScan({"com.CricleDependent"})
10
+@EnableAspectJAutoProxy
11
+@EnableAsync
12
+public class Config {
13
+
14
+}

+ 22 - 0
spring_aop/src/main/java/com/CricleDependent/DependentAspect.java

@@ -0,0 +1,22 @@
1
+package com.CricleDependent;
2
+
3
+import org.aspectj.lang.ProceedingJoinPoint;
4
+import org.aspectj.lang.annotation.Around;
5
+import org.aspectj.lang.annotation.Aspect;
6
+import org.aspectj.lang.annotation.Pointcut;
7
+import org.springframework.stereotype.Component;
8
+
9
+@Aspect
10
+@Component
11
+public class DependentAspect {
12
+    @Pointcut("execution(* com.CricleDependent.*.*(..))")
13
+    public void setPointCut(){}
14
+
15
+    @Around("setPointCut()")
16
+    public Object Around(ProceedingJoinPoint joinPoint) throws Throwable {
17
+        System.out.println(1);
18
+        Object proceed = joinPoint.proceed();
19
+        System.out.println(2);
20
+        return null;
21
+    }
22
+}

+ 13 - 0
spring_aop/src/main/java/com/CricleDependent/Main.java

@@ -0,0 +1,13 @@
1
+package com.CricleDependent;
2
+
3
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4
+
5
+public class Main {
6
+    public static void main(String[] args) {
7
+        AnnotationConfigApplicationContext an = new AnnotationConfigApplicationContext(Config.class);
8
+        AService aBean = an.getBean(AService.class);
9
+        BService bBean = an.getBean(BService.class);
10
+        aBean.test();
11
+        bBean.test();
12
+    }
13
+}

+ 14 - 0
spring_aop/src/main/java/com/FactoryBeanTest.java

@@ -0,0 +1,14 @@
1
+package com;
2
+
3
+import com.aop.MyAspect;
4
+import org.springframework.context.support.ClassPathXmlApplicationContext;
5
+
6
+public class FactoryBeanTest {
7
+    public static void main(String[] args) {
8
+        ClassPathXmlApplicationContext classPathXmlApplicationContext =
9
+                new ClassPathXmlApplicationContext("classpath:context.xml");
10
+        Object myFactoryUserBean1 = classPathXmlApplicationContext.getBean("myFactoryBean");
11
+        Object myFactoryUserBean2 = classPathXmlApplicationContext.getBean("myFactoryBean");
12
+        
13
+    }
14
+}

+ 13 - 0
spring_aop/src/main/java/com/SpringTest.java

@@ -0,0 +1,13 @@
1
+package com;
2
+
3
+import com.aop.MyAspect;
4
+import com.aop.TargetImpl;
5
+import org.springframework.context.support.ClassPathXmlApplicationContext;
6
+
7
+public class SpringTest {
8
+    public static void main(String[] args) {
9
+        ClassPathXmlApplicationContext classPathXmlApplicationContext =
10
+                new ClassPathXmlApplicationContext("classpath:context.xml");
11
+//        MyAspect target = (MyAspect) classPathXmlApplicationContext.getBean("myaspect");
12
+    }
13
+}

+ 5 - 0
spring_aop/src/main/java/com/TestTx.java

@@ -1,10 +1,12 @@
1
 package com;
1
 package com;
2
 
2
 
3
 
3
 
4
+import com.config.NameConfig;
4
 import com.transaction_anno.service.AccountTxService;
5
 import com.transaction_anno.service.AccountTxService;
5
 import org.junit.Test;
6
 import org.junit.Test;
6
 import org.junit.runner.RunWith;
7
 import org.junit.runner.RunWith;
7
 import org.springframework.beans.factory.annotation.Autowired;
8
 import org.springframework.beans.factory.annotation.Autowired;
9
+import org.springframework.context.annotation.ImportResource;
8
 import org.springframework.test.annotation.Commit;
10
 import org.springframework.test.annotation.Commit;
9
 import org.springframework.test.context.ContextConfiguration;
11
 import org.springframework.test.context.ContextConfiguration;
10
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -12,9 +14,12 @@ import org.springframework.transaction.annotation.Transactional;
12
 
14
 
13
 @RunWith(SpringJUnit4ClassRunner.class)
15
 @RunWith(SpringJUnit4ClassRunner.class)
14
 @ContextConfiguration("classpath:context.xml")
16
 @ContextConfiguration("classpath:context.xml")
17
+//@ImportResource("classpath:context.xml")
15
 public class TestTx {
18
 public class TestTx {
16
     @Autowired
19
     @Autowired
17
     AccountTxService accountTxService;
20
     AccountTxService accountTxService;
21
+    @Autowired
22
+    NameConfig nameConfig;
18
     @Test
23
     @Test
19
     public void transformAnno() {
24
     public void transformAnno() {
20
         try {
25
         try {

+ 11 - 1
spring_aop/src/main/java/com/anno/TargetImpl.java

@@ -1,9 +1,19 @@
1
 package com.anno;
1
 package com.anno;
2
 
2
 
3
+import com.anno.service.MyService;
4
+import org.springframework.beans.factory.BeanNameAware;
5
+import org.springframework.beans.factory.annotation.Autowired;
3
 import org.springframework.stereotype.Component;
6
 import org.springframework.stereotype.Component;
4
 
7
 
5
 @Component("ttaget")
8
 @Component("ttaget")
6
-public class TargetImpl implements TargetInterface {
9
+public class TargetImpl implements TargetInterface, BeanNameAware {
10
+    @Override
11
+    public void setBeanName(String s) {
12
+        System.out.println("beanName"+ s);
13
+    }
14
+
15
+    @Autowired
16
+    private MyService myService;
7
     @Override
17
     @Override
8
     public void save() {
18
     public void save() {
9
         System.out.println("target save");
19
         System.out.println("target save");

+ 7 - 0
spring_aop/src/main/java/com/anno/service/MyService.java

@@ -0,0 +1,7 @@
1
+package com.anno.service;
2
+
3
+import org.springframework.stereotype.Service;
4
+
5
+@Service
6
+public class MyService {
7
+}

+ 21 - 0
spring_aop/src/main/java/com/config/MyBeanProcessor.java

@@ -0,0 +1,21 @@
1
+package com.config;
2
+
3
+import org.springframework.beans.BeansException;
4
+import org.springframework.beans.factory.config.BeanPostProcessor;
5
+import org.springframework.stereotype.Component;
6
+
7
+@Component
8
+public class MyBeanProcessor implements BeanPostProcessor {
9
+    @Override
10
+    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
11
+        if(beanName.equals("ttaget")) {
12
+            System.out.println("before");
13
+        }
14
+        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
15
+    }
16
+
17
+    @Override
18
+    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
19
+        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
20
+    }
21
+}

+ 12 - 0
spring_aop/src/main/java/com/config/NameConfig.java

@@ -0,0 +1,12 @@
1
+package com.config;
2
+
3
+import org.springframework.beans.factory.annotation.Value;
4
+import org.springframework.context.annotation.PropertySource;
5
+import org.springframework.stereotype.Component;
6
+
7
+@PropertySource("classpath:self_design.properties")
8
+@Component
9
+public class NameConfig {
10
+    @Value("${name.first}")
11
+    String jdbcUrl;
12
+}

+ 24 - 0
spring_aop/src/main/java/com/factory_bean/MyFactoryBean.java

@@ -0,0 +1,24 @@
1
+package com.factory_bean;
2
+
3
+import org.springframework.beans.factory.FactoryBean;
4
+import org.springframework.stereotype.Component;
5
+
6
+@Component
7
+public class MyFactoryBean implements FactoryBean<MyUser> {
8
+    @Override
9
+    public MyUser getObject() throws Exception {
10
+        MyUser myUser = new MyUser();
11
+        myUser.setMyUserName("myUserName");
12
+        return myUser;
13
+    }
14
+
15
+    @Override
16
+    public Class<?> getObjectType() {
17
+        return null;
18
+    }
19
+
20
+    @Override
21
+    public boolean isSingleton() {
22
+        return true;
23
+    }
24
+}

+ 8 - 0
spring_aop/src/main/java/com/factory_bean/MyUser.java

@@ -0,0 +1,8 @@
1
+package com.factory_bean;
2
+
3
+import lombok.Data;
4
+
5
+@Data
6
+public class MyUser {
7
+    private String myUserName;
8
+}

+ 2 - 1
spring_aop/src/main/java/com/transaction_anno/service/impl/AccountServiceImpl.java

@@ -21,7 +21,7 @@ public class AccountServiceImpl implements AccountService {
21
         int i = accountDao.saveAccount(accountEntity);
21
         int i = accountDao.saveAccount(accountEntity);
22
     }
22
     }
23
 //    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
23
 //    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
24
-    @Transactional
24
+    @Transactional(propagation = Propagation.REQUIRES_NEW)
25
     @Override
25
     @Override
26
     public int reduceAccount(AccountEntity toEntity) {
26
     public int reduceAccount(AccountEntity toEntity) {
27
         int in = accountDao.incomeAccount(toEntity, "to");
27
         int in = accountDao.incomeAccount(toEntity, "to");
@@ -29,6 +29,7 @@ public class AccountServiceImpl implements AccountService {
29
         }catch (Exception e) {
29
         }catch (Exception e) {
30
             System.out.println(e.getMessage());
30
             System.out.println(e.getMessage());
31
         }*/
31
         }*/
32
+        int i = 1/0;
32
         return in;
33
         return in;
33
     }
34
     }
34
 }
35
 }

+ 2 - 1
spring_aop/src/main/java/com/transaction_anno/service/impl/AccountTwoServiceImpl.java

@@ -15,6 +15,7 @@ public class AccountTwoServiceImpl implements AccountTwoService {
15
     @Transactional(rollbackFor = Exception.class)
15
     @Transactional(rollbackFor = Exception.class)
16
     public void addAccountBalance(AccountEntity addEntity) {
16
     public void addAccountBalance(AccountEntity addEntity) {
17
         int in = accountDao.incomeAccount(addEntity, "in");
17
         int in = accountDao.incomeAccount(addEntity, "in");
18
-        throw new RuntimeException("runtimeERROR");
18
+
19
+//        throw new RuntimeException("runtimeERROR");
19
     }
20
     }
20
 }
21
 }

+ 5 - 1
spring_aop/src/main/java/com/transaction_anno/service/impl/AccountTxServiceImpl.java

@@ -25,7 +25,11 @@ public class AccountTxServiceImpl implements AccountTxService {
25
         AccountEntity addEntity = new AccountEntity();
25
         AccountEntity addEntity = new AccountEntity();
26
         reduceEntity.setId(16).setBalance(3.0);
26
         reduceEntity.setId(16).setBalance(3.0);
27
         addEntity.setId(17).setBalance(3.0);
27
         addEntity.setId(17).setBalance(3.0);
28
-        accountService.reduceAccount(reduceEntity);
29
         accountTwoService.addAccountBalance(addEntity);
28
         accountTwoService.addAccountBalance(addEntity);
29
+        try{
30
+            accountService.reduceAccount(reduceEntity);
31
+        } catch (Exception e) {
32
+            System.out.println(e.getMessage());
33
+        }
30
     }
34
     }
31
 }
35
 }

+ 1 - 0
spring_aop/src/main/resources/context.xml

@@ -18,6 +18,7 @@
18
     <bean id="target" class="com.aop.TargetImpl"></bean>
18
     <bean id="target" class="com.aop.TargetImpl"></bean>
19
     <bean id="myaspect" class="com.aop.MyAspect">
19
     <bean id="myaspect" class="com.aop.MyAspect">
20
     </bean>
20
     </bean>
21
+    <bean id="myFactoryUserBean"  class="com.factory_bean.MyFactoryBean"></bean>
21
     <aop:config>
22
     <aop:config>
22
         <!-- 声明切面-->
23
         <!-- 声明切面-->
23
         <aop:aspect ref="myaspect">
24
         <aop:aspect ref="myaspect">

+ 2 - 0
spring_aop/src/main/resources/self_design.properties

@@ -0,0 +1,2 @@
1
+name.first=zhang
2
+name.second=xiao,yu