pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!--spring整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.6.RELEASE</version>
<scope>test</scope>
</dependency>
<!--spring整合web环境-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!--java工具类,SpringUtils-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.8</version>
</dependency>

配置类

MyWebApplicationInitializer.java

1
2
3
4
5
6
7
8
9
10
11
// 等价于web.xml配置文件
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Load Spring web application configuration
AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
ac.register(SpringConfig.class);
ac.refresh();
StaticLog.info("初始化spring容器");
}
}

spring配置类

1
2
3
4
5
6
// spring配置类
@Configuration
@ComponentScan(value = {"cn.itcast","cn.hutool.extra.spring"}) //cn.itcast 自己程序的包,
public class SpringConfig {

}

使用

1
Demo2 testDemo = SpringUtil.getBean("testDemo");

__END__

三国小梦
文章作者:三国小梦
文章出处Servlet+spring 纯java配置(无xml)
作者签名:简单地活着, 肆意又精彩.
关于主题Hexo - Live For Code
版权声明:文章除特别声明外,均采用 BY-NC-SA 许可协议,转载请注明出处