• Java注解式开发


    目录

     

    1. ssm框架的搭建 

    1.1 maven项目的创建

    1.2 导入所需的包到pom.xml文件中

    1.3 导入数据库连接文件、日志文件、redis连接文件

    1.3.1 数据库连接文件

    1.3.2 日志文件

    1.3.3 redis连接文件

    1.4 导入spring-mybatis、spring-mvc、spring-base、spring-redis四种集成文件

    1.4.1 spring-mybatis集成文件

    1.4.2 spring-mvc集成文件

    1.4.3 spring-base集成文件

    1.4.4 spring-redis集成文件

    1.5 导入web.xml

    1.6 导入所需的工具包(util)文件

    ·1.6.1 适用于支持java web应用跨资源源共享(CORS)的首个统一解决方案的文件

    1.6.2 

    1.6.3 处理请求编码问题文件

    1.6.4 继承了感知器

    1.7 运行结果

    2. 获取注解的参数


    1. ssm框架的搭建 


    1.1 maven项目的创建


    注:将jkd版本切换至1.8,web.xml的版本切换至3.1

    1.2 导入所需的包到pom.xml文件中


    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <groupId>com.naragroupId>
    6. <artifactId>ssmdemoartifactId>
    7. <version>1.0-SNAPSHOTversion>
    8. <packaging>warpackaging>
    9. <name>ssmdemo Maven Webappname>
    10. <url>http://www.example.comurl>
    11. <properties>
    12. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    13. <maven.compiler.source>1.8maven.compiler.source>
    14. <maven.compiler.target>1.8maven.compiler.target>
    15. properties>
    16. <dependencies>
    17. <dependency>
    18. <groupId>junitgroupId>
    19. <artifactId>junitartifactId>
    20. <version>4.12version>
    21. <scope>testscope>
    22. dependency>
    23. <dependency>
    24. <groupId>org.springframeworkgroupId>
    25. <artifactId>spring-contextartifactId>
    26. <version>5.3.18version>
    27. dependency>
    28. <dependency>
    29. <groupId>org.springframeworkgroupId>
    30. <artifactId>spring-txartifactId>
    31. <version>5.3.18version>
    32. dependency>
    33. <dependency>
    34. <groupId>org.springframeworkgroupId>
    35. <artifactId>spring-jdbcartifactId>
    36. <version>5.3.18version>
    37. dependency>
    38. <dependency>
    39. <groupId>org.springframeworkgroupId>
    40. <artifactId>spring-testartifactId>
    41. <version>5.3.18version>
    42. dependency>
    43. <dependency>
    44. <groupId>org.springframeworkgroupId>
    45. <artifactId>spring-webartifactId>
    46. <version>5.3.18version>
    47. dependency>
    48. <dependency>
    49. <groupId>org.springframeworkgroupId>
    50. <artifactId>spring-webmvcartifactId>
    51. <version>5.3.18version>
    52. dependency>
    53. <dependency>
    54. <groupId>org.springframeworkgroupId>
    55. <artifactId>spring-ormartifactId>
    56. <version>5.3.18version>
    57. dependency>
    58. <dependency>
    59. <groupId>org.springframeworkgroupId>
    60. <artifactId>spring-aspectsartifactId>
    61. <version>5.3.18version>
    62. dependency>
    63. <dependency>
    64. <groupId>org.mybatisgroupId>
    65. <artifactId>mybatisartifactId>
    66. <version>3.5.6version>
    67. dependency>
    68. <dependency>
    69. <groupId>org.mybatisgroupId>
    70. <artifactId>mybatis-springartifactId>
    71. <version>2.0.6version>
    72. dependency>
    73. <dependency>
    74. <groupId>com.github.pagehelpergroupId>
    75. <artifactId>pagehelperartifactId>
    76. <version>5.1.2version>
    77. dependency>
    78. <dependency>
    79. <groupId>mysqlgroupId>
    80. <artifactId>mysql-connector-javaartifactId>
    81. <version>5.1.44version>
    82. dependency>
    83. <dependency>
    84. <groupId>org.apache.commonsgroupId>
    85. <artifactId>commons-dbcp2artifactId>
    86. <version>2.1.1version>
    87. dependency>
    88. <dependency>
    89. <groupId>org.apache.commonsgroupId>
    90. <artifactId>commons-pool2artifactId>
    91. <version>2.4.3version>
    92. dependency>
    93. <dependency>
    94. <groupId>org.slf4jgroupId>
    95. <artifactId>slf4j-apiartifactId>
    96. <version>1.7.36version>
    97. dependency>
    98. <dependency>
    99. <groupId>ch.qos.logbackgroupId>
    100. <artifactId>logback-classicartifactId>
    101. <version>1.2.10version>
    102. dependency>
    103. <dependency>
    104. <groupId>redis.clientsgroupId>
    105. <artifactId>jedisartifactId>
    106. <version>2.9.0version>
    107. dependency>
    108. <dependency>
    109. <groupId>org.springframework.datagroupId>
    110. <artifactId>spring-data-redisartifactId>
    111. <version>1.7.2.RELEASEversion>
    112. dependency>
    113. <dependency>
    114. <groupId>com.fasterxml.jackson.coregroupId>
    115. <artifactId>jackson-coreartifactId>
    116. <version>2.9.4version>
    117. dependency>
    118. <dependency>
    119. <groupId>com.fasterxml.jackson.coregroupId>
    120. <artifactId>jackson-databindartifactId>
    121. <version>2.9.4version>
    122. dependency>
    123. <dependency>
    124. <groupId>org.projectlombokgroupId>
    125. <artifactId>lombokartifactId>
    126. <version>1.18.22version>
    127. <scope>providedscope>
    128. dependency>
    129. <dependency>
    130. <groupId>javax.servletgroupId>
    131. <artifactId>javax.servlet-apiartifactId>
    132. <version>3.1.0version>
    133. <scope>providedscope>
    134. dependency>
    135. dependencies>
    136. <build>
    137. <finalName>ssmdemofinalName>
    138. <pluginManagement>
    139. <plugins>
    140. <plugin>
    141. <artifactId>maven-clean-pluginartifactId>
    142. <version>3.1.0version>
    143. plugin>
    144. <plugin>
    145. <artifactId>maven-resources-pluginartifactId>
    146. <version>3.0.2version>
    147. plugin>
    148. <plugin>
    149. <artifactId>maven-compiler-pluginartifactId>
    150. <version>3.8.0version>
    151. plugin>
    152. <plugin>
    153. <artifactId>maven-surefire-pluginartifactId>
    154. <version>2.22.1version>
    155. plugin>
    156. <plugin>
    157. <artifactId>maven-war-pluginartifactId>
    158. <version>3.2.2version>
    159. plugin>
    160. <plugin>
    161. <artifactId>maven-install-pluginartifactId>
    162. <version>2.5.2version>
    163. plugin>
    164. <plugin>
    165. <artifactId>maven-deploy-pluginartifactId>
    166. <version>2.8.2version>
    167. plugin>
    168. plugins>
    169. pluginManagement>
    170. build>
    171. project>

    1.3 导入数据库连接文件、日志文件、redis连接文件


    1.3.1 数据库连接文件


    1. jdbc.driver=com.mysql.jdbc.Driver
    2. jdbc.url=jdbc:mysql://localhost:3306/tmpdb?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
    3. jdbc.username=root
    4. jdbc.password=123456

    注:tmpdb是数据库的名称 

    1.3.2 日志文件


    1. "1.0" encoding="UTF-8"?>
    2. <configuration debug="false">
    3. <property name="LOG_HOME" value="d:\\temp\\mylogs" />
    4. <property name="LOG_NAME" value="ssm"/>
    5. <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    6. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    7. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
    8. encoder>
    9. appender>
    10. <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    11. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    12. <FileNamePattern>${LOG_HOME}/${LOG_NAME}.%d{yyyy-MM-dd}.logFileNamePattern>
    13. <MaxHistory>30MaxHistory>
    14. rollingPolicy>
    15. <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    16. <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
    17. encoder>
    18. <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
    19. <MaxFileSize>10MBMaxFileSize>
    20. triggeringPolicy>
    21. appender>
    22. <logger name="com.apache.ibatis" level="TRACE"/>
    23. <logger name="java.sql.Connection" level="DEBUG"/>
    24. <logger name="java.sql.Statement" level="DEBUG"/>
    25. <logger name="java.sql.PreparedStatement" level="DEBUG"/>
    26. <root level="DEBUG">
    27. <appender-ref ref="STDOUT" />
    28. <appender-ref ref="FILE"/>
    29. root>
    30. configuration>

    1.3.3 redis连接文件


    1. redis.host=localhost
    2. redis.port=6379
    3. redis.maxTotal=10
    4. redis.maxIdle=5
    5. redis.testOnBorrow=true

    1.4 导入spring-mybatis、spring-mvc、spring-base、spring-redis四种集成文件


    1.4.1 spring-mybatis集成文件


    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    4. xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    9. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    10. <property name="dataSource" ref="dataSource"/>
    11. <property name="mapperLocations" value="classpath:/mapper/**/*.xml"/>
    12. <property name="plugins">
    13. <list>
    14. <bean class="com.github.pagehelper.PageInterceptor">
    15. <property name="properties">
    16. <value>
    17. helperDialect=mysql
    18. value>
    19. property>
    20. bean>
    21. list>
    22. property>
    23. bean>
    24. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    25. <property name="basePackage" value="com/zking/**/mapper"/>
    26. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    27. bean>
    28. beans>

    1.4.2 spring-mvc集成文件


    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:mvc="http://www.springframework.org/schema/mvc"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    8. <context:component-scan base-package="com.zking" use-default-filters="false">
    9. <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    10. context:component-scan>
    11. <mvc:annotation-driven>mvc:annotation-driven>
    12. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    13. <property name="viewClass"
    14. value="org.springframework.web.servlet.view.JstlView">property>
    15. <property name="prefix" value="/WEB-INF/jsp/"/>
    16. <property name="suffix" value=".jsp"/>
    17. bean>
    18. <mvc:resources location="/static/" mapping="/static/**" cache-period="31536000"/>
    19. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    20. <property name="messageConverters">
    21. <list>
    22. <ref bean="mappingJackson2HttpMessageConverter"/>
    23. list>
    24. property>
    25. bean>
    26. <bean id="mappingJackson2HttpMessageConverter"
    27. class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    28. <property name="supportedMediaTypes">
    29. <list>
    30. <value>text/html;charset=UTF-8value>
    31. <value>text/json;charset=UTF-8value>
    32. <value>application/json;charset=UTF-8value>
    33. list>
    34. property>
    35. bean>
    36. beans>

    1.4.3 spring-base集成文件


    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    4. xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    9. <context:component-scan base-package="com.zking"/>
    10. <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
    11. <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
    12. destroy-method="close">
    13. <property name="driverClassName" value="${jdbc.driver}"/>
    14. <property name="url" value="${jdbc.url}"/>
    15. <property name="username" value="${jdbc.username}"/>
    16. <property name="password" value="${jdbc.password}"/>
    17. <property name="initialSize" value="10"/>
    18. <property name="maxTotal" value="100"/>
    19. <property name="maxIdle" value="50"/>
    20. <property name="minIdle" value="10"/>
    21. <property name="maxWaitMillis" value="-1"/>
    22. <property name="testWhileIdle" value="true">property>
    23. <property name="validationQuery" value="select 1">property>
    24. bean>
    25. <aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true"/>
    26. <bean id="transactionManager"
    27. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    28. <property name="dataSource" ref="dataSource"/>
    29. bean>
    30. <tx:annotation-driven transaction-manager="transactionManager"/>
    31. beans>

    1.4.4 spring-redis集成文件


    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    4. xmlns:cache="http://www.springframework.org/schema/cache"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    6. <context:property-placeholder location="classpath:redis.properties"
    7. ignore-unresolvable="true"/>
    8. <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
    9. <property name="maxTotal" value="${redis.maxTotal}" />
    10. <property name="maxIdle" value="${redis.maxIdle}" />
    11. <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    12. bean>
    13. <bean id="jedisConnectionFactory"
    14. class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    15. <property name="hostName" value="${redis.host}">property>
    16. <property name="port" value="${redis.port}">property>
    17. <property name="poolConfig" ref="poolConfig">property>
    18. bean>
    19. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    20. <property name="connectionFactory" ref="jedisConnectionFactory" />
    21. <property name="keySerializer">
    22. <bean
    23. class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    24. property>
    25. <property name="valueSerializer">
    26. <bean
    27. class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
    28. property>
    29. <property name="hashKeySerializer">
    30. <bean
    31. class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    32. property>
    33. <property name="hashValueSerializer">
    34. <bean
    35. class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
    36. property>
    37. <property name="enableTransactionSupport" value="true">
    38. property>
    39. bean>
    40. <cache:annotation-driven order="2" cache-manager="cacheManager" key-generator="customKeyGenerator"/>
    41. <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
    42. <constructor-arg index="0" ref="redisTemplate">constructor-arg>
    43. bean>
    44. <bean id="customKeyGenerator" class="com.zking.ssmdemo.util.CustomKeyGenerator"/>
    45. beans>

    1.5 导入web.xml


    1. "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_3_1.xsd"
    5. version="3.1">
    6. <context-param>
    7. <param-name>contextConfigLocationparam-name>
    8. <param-value>classpath*:spring-*.xmlparam-value>
    9. context-param>
    10. <listener>
    11. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    12. listener>
    13. <filter>
    14. <filter-name>encodingFilterfilter-name>
    15. <filter-class>com.zking.ssmdemo.util.EncodingFilterfilter-class>
    16. filter>
    17. <filter-mapping>
    18. <filter-name>encodingFilterfilter-name>
    19. <url-pattern>/*url-pattern>
    20. filter-mapping>
    21. <filter>
    22. <filter-name>corsFilterfilter-name>
    23. <filter-class>com.zking.ssmdemo.util.CorsFilterfilter-class>
    24. filter>
    25. <filter-mapping>
    26. <filter-name>corsFilterfilter-name>
    27. <url-pattern>/*url-pattern>
    28. filter-mapping>
    29. <servlet>
    30. <servlet-name>SpringMVCservlet-name>
    31. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    32. <init-param>
    33. <param-name>contextConfigLocationparam-name>
    34. <param-value>classpath*:spring-mvc.xmlparam-value>
    35. init-param>
    36. <load-on-startup>1load-on-startup>
    37. <async-supported>trueasync-supported>
    38. servlet>
    39. <servlet-mapping>
    40. <servlet-name>SpringMVCservlet-name>
    41. <url-pattern>/url-pattern>
    42. servlet-mapping>
    43. web-app>

    1.6 导入所需的工具包(util)文件


    ·1.6.1 适用于支持java web应用跨资源源共享(CORS)的首个统一解决方案的文件


    1. package com.zking.ssmdemo.util;
    2. import javax.servlet.*;
    3. import javax.servlet.http.HttpServletRequest;
    4. import javax.servlet.http.HttpServletResponse;
    5. import java.io.IOException;
    6. /**
    7. * 配置tomcat允许跨域访问
    8. *
    9. * @author Administrator
    10. *
    11. */
    12. public class CorsFilter implements Filter {
    13. @Override
    14. public void init(FilterConfig filterConfig) throws ServletException {
    15. }
    16. @Override
    17. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
    18. throws IOException, ServletException {
    19. HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
    20. HttpServletRequest req = (HttpServletRequest) servletRequest;
    21. // Access-Control-Allow-Origin就是我们需要设置的域名
    22. // Access-Control-Allow-Headers跨域允许包含的头。
    23. // Access-Control-Allow-Methods是允许的请求方式
    24. httpResponse.setHeader("Access-Control-Allow-Origin", "*");// *,任何域名
    25. httpResponse.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE");
    26. httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept,jwt");
    27. //允许客户端,处理一个新的响应头jwt
    28. httpResponse.setHeader("Access-Control-Expose-Headers", "jwt");
    29. // axios的ajax会发两次请求,第一次提交方式为:option,直接返回即可
    30. if ("OPTIONS".equals(req.getMethod())) {
    31. return;
    32. }
    33. filterChain.doFilter(servletRequest, servletResponse);
    34. }
    35. @Override
    36. public void destroy() {
    37. }
    38. }

    1.6.2 


    1. package com.zking.ssmdemo.util;
    2. import org.springframework.cache.interceptor.KeyGenerator;
    3. import java.lang.reflect.Method;
    4. public class CustomKeyGenerator implements KeyGenerator {
    5. @Override
    6. public Object generate(Object target, Method method, Object... params) {
    7. StringBuilder sb = new StringBuilder();
    8. sb.append(target.getClass().getName());
    9. sb.append(method.getName());
    10. for(Object param: params) {
    11. sb.append(param.toString());
    12. }
    13. return sb;
    14. }
    15. }

    1.6.3 处理请求编码问题文件


    1. package com.zking.ssmdemo.util;
    2. import javax.servlet.*;
    3. import javax.servlet.http.HttpServletRequest;
    4. import javax.servlet.http.HttpServletResponse;
    5. import java.io.IOException;
    6. /**
    7. * 中文乱码处理
    8. *
    9. */
    10. public class EncodingFilter implements Filter {
    11. private String encoding = "UTF-8";// 默认字符集
    12. public EncodingFilter() {
    13. super();
    14. }
    15. public void destroy() {
    16. }
    17. public void doFilter(ServletRequest request, ServletResponse response,
    18. FilterChain chain) throws IOException, ServletException {
    19. HttpServletRequest req = (HttpServletRequest) request;
    20. HttpServletResponse res = (HttpServletResponse) response;
    21. // 中文处理必须放到 chain.doFilter(request, response)方法前面
    22. res.setContentType("text/html;charset=" + this.encoding);
    23. if (req.getMethod().equalsIgnoreCase("post")) {
    24. req.setCharacterEncoding(this.encoding);
    25. }
    26. chain.doFilter(request, response);
    27. }
    28. public void init(FilterConfig filterConfig) throws ServletException {
    29. String s = filterConfig.getInitParameter("encoding");// 读取web.xml文件中配置的字符集
    30. if (null != s && !s.trim().equals("")) {
    31. this.encoding = s.trim();
    32. }
    33. }
    34. }

    1.6.4 继承了感知器


    1. package com.zking.ssmdemo.util;
    2. import org.springframework.beans.BeansException;
    3. import org.springframework.context.ApplicationContext;
    4. import org.springframework.context.ApplicationContextAware;
    5. import org.springframework.stereotype.Component;
    6. /**
    7. * @author L
    8. * @site www.xiaomage.com
    9. * @company xxx公司
    10. * @create  2022-11-09 15:40
    11. */
    12. @Component
    13. public class SpringUtils implements ApplicationContextAware {
    14. private static ApplicationContext ctx;
    15. @Override
    16. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    17. ctx = applicationContext;
    18. }
    19. public static <T> T getBean(String id) {
    20. return (T)ctx.getBean(id);
    21. }
    22. }

    1.7 运行结果


    自定义的注解也有(Mylog AOP。。。)

    2. 获取注解的参数


     value,name两个自定义参数

  • 相关阅读:
    使用 Nuxt 构建简单后端接口及数据库数据请求
    从零玩转人脸识别
    【c++学习笔记】-1 基础
    [附源码]计算机毕业设计网约车智能接单规划小程序Springboot程序
    《六月集训》(第二十二天)——有序集合
    机器学习6scikit-learn中的scaler
    在用户同意隐私政策前,您的应用获取了用户的ANDROID ID,不符合华为应用市场审核标准。
    cmake: command not found
    Vue3.3指北(三)
    史上最全 Zuul网关鉴权 范文
  • 原文地址:https://blog.csdn.net/qq_62881798/article/details/127811305