spring纯java注解式开发(一)
习惯了用XML文件来配置spring,现在开始尝试使用纯java代码来配置spring。
其实,spring的纯java配置,简单来说就是将bean标签的内容通过注解转换成bean对象的过程,没什么神秘的地方。
首先来配置AppConfig文件:
配置的英文叫做configuration,所以,java配置文件的类前,为了说明此类属于配置文件的范畴,就加上这样一个标签:@Configuration 用来标识此类是一个配置类;然后就是@ComponentScan 标签,是不是很熟悉?对的,这个就是表示扫描范围的一个标签,后面可以加上一个属性 basePackages 用来说明要管理的bean在哪个包下,使用方式和在XML文件里配置时的使用方法一样,如果是多包扫面,就用大括号括起来,中间用逗号隔开就行了;其他的根据需要进行添加,譬如 @EnableScheduling 和 @EnableAspectJAutoProxy 等等,按需添加即可。
在这个文件里呢,我配置了一个数据库。第一,通过@Autowired 来注入DataSource ,然后配置一个@Bean,就是写一个datasource的实例就ok了。其他的,譬如shiro的配置等等都可以在这里进行,方法同data一样。定义新拦截器也可在此处进行,需要说明的就是,拦截器的bean里需要添加一个name属性,用来定义此拦截器的名称,方便在web.xml中进行引用。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.util.FileCopyUtils; import javax.servlet.Filter;
import javax.sql.DataSource;
import java.io.FileReader;
import java.io.IOException; @Configuration
@EnableScheduling
@EnableAspectJAutoProxy
@ComponentScan(basePackages = {"com.lab.service", "com.lab.task", "com.lab.security"})
public class ApplicationConfig { @Autowired
private DataSource dataSource; @Bean
public IDBI database() {
IDBI dbi = new DBI(dataSource);
logger.debug("dbi : {}", dbi);
return dbi;
} // 。。。 }
其次就是配置WebConfig文件:
同前一个一样,首先需要添加的就是@Configuration 标签,还有一个不同的就是需要加上@EnableWebMvc 标签以开启MVC模式。当然也有@ComponentScan 标签,使用方法同前,需要basePackages 属性的定义。按需也可添加诸如 @EnableAspectJAutoProxy 等标签。
这里定义的有譬如 setPrefix 和 setSuffix 等内容,具体按实际进行增减。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration
@EnableWebMvc
@EnableAspectJAutoProxy
@ComponentScan(basePackages = { "com.lab.controller" })
public class WebConfig extends WebMvcConfigurerAdapter { @Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/view/");
resolver.setSuffix(".jsp");
return resolver;
} }
定义DataConfig文件:
前面我是直接在AppConfig中进行注入了数据库的定义,是因为具体的数据库连接的定义我是在这里进行的。
@Configuration 必不可少了,然后我又定义了一个属性 @Profile(“data”)用以说明此类的用途。然后就是定义了一个@Bean(name=“dataSource”) ,内容是数据库连接池、链接的用户名、密码、等待超时时间啦等等一系列的数据库的配置。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile; import com.alibaba.druid.pool.DruidDataSource; @Configuration
@Profile("data")
public class DevConfig {
/* 此处我用了properties文件的方式进行数据库的定义,也可直接在代码中书写 */
private static final String databaseConfig = "datasource.properties"; @Bean(name = "dataSource")
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
InputStream inStream = null;
try {
Properties prop = new Properties();
inStream = getClass().getClassLoader().getResourceAsStream(databaseConfig);
prop.load(inStream);
String datastyle = prop.getProperty("data.style");
dataSource.setUrl(
prop.getProperty(datastyle + ".data.url") + ":" + prop.getProperty(datastyle + ".data.database"));
dataSource.setUsername(prop.getProperty(datastyle + ".data.user"));
dataSource.setPassword(prop.getProperty(datastyle + ".data.password"));
/* 配置过滤 */
dataSource.setFilters(prop.getProperty(datastyle + ".data.filters"));
/* 配置初始化大小、最小、最大 */
dataSource.setInitialSize(Integer.parseInt(prop.getProperty(datastyle + ".data.initialSize")));
dataSource.setMinIdle(Integer.parseInt(prop.getProperty(datastyle + ".data.minIdle")));
dataSource.setMaxActive(Integer.parseInt(prop.getProperty(datastyle + ".data.maxActive")));
/* 配置获取连接等待超时的时间 */
dataSource.setMaxWait(Integer.parseInt(prop.getProperty(datastyle + ".data.maxWait")));
/* 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 */
dataSource.setTimeBetweenEvictionRunsMillis(
Integer.parseInt(prop.getProperty(datastyle + ".data.timeBetweenEvictionRunsMillis")));
/* 配置一个连接在池中最小生存的时间,单位是毫秒 */
dataSource.setMinEvictableIdleTimeMillis(
Integer.parseInt(prop.getProperty(datastyle + ".data.minEvictableIdleTimeMillis")));
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
} catch (IOException iOException) {
iOException.printStackTrace();
} catch (SQLException sQLException) {
sQLException.printStackTrace();
}
return dataSource;
} }
这些都定义好后,在web.xml文件中进行配置下就行了。
<!-- 上下文配置文件の地址 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
com.bell.lab.springconfig.DevConfig,
com.bell.lab.springconfig.ApplicationConfig
</param-value>
</context-param>
<servlet>
<servlet-name>SpringDemoServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.bell.lab.springconfig.WebConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringDemoServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>data</param-value>
</context-param>
至此完事,跟在spring-application.xml中进行配置效果是一样的,整体上更符合java的习惯而已。
ps:其实,就算是web.xml文件,也可通过java代码的形式进行配置的,不过我觉着有点麻烦,就没进行说明,感兴趣的可以自行查找相关资料进行配置。
spring纯java注解式开发(一)的更多相关文章
- Spring常用注解式开发
1.组件注册@Configuration.@Bean给容器中注册组件. 注解,@Configuration告诉Spring这是一个配置类,相当于bean.xml配置文件. 注解,@Bean给Sprin ...
- Spring MVC (二)注解式开发使用详解
MVC注解式开发即处理器基于注解的类开发, 对于每一个定义的处理器, 无需在xml中注册. 只需在代码中通过对类与方法的注解, 即可完成注册. 定义处理器 @Controller: 当前类为处理器 @ ...
- Spring MVC注解式开发
MVC注解式开发即处理器基于注解的类开发, 对于每一个定义的处理器, 无需在xml中注册. 只需在代码中通过对类与方法的注解, 即可完成注册. 定义处理器 @Controller: 当前类为处理器 @ ...
- 总结切面编程AOP的注解式开发和XML式开发
有段日子没有总结东西了,因为最近确实有点忙,一直在忙于hadoop集群的搭建,磕磕碰碰现在勉强算是能呼吸了,因为这都是在自己的PC上,资源确实有点紧张(搭建过程后期奉上),今天难得大家都有空(哈哈哈~ ...
- 3.2.3 SpringMVC注解式开发
SpringMVC注解式开发 1. 搭建环境 (1) 后端控制器无需实现接口 , 添加相应注解 Controller类添加注解 @Controller //该注解表将当前类交给spring容器管理 @ ...
- SpringMVC笔记:annotation注解式开发
一.配置式开发 在我们之前的学习中,springmvc使用配置式开发模式,需要在核心配置文件中springmvc.xml注册大量的bean来注入controller控制器,工作繁琐容易出错,下面我们学 ...
- shiro授权、注解式开发
在ShiroUserMapper.xml中新增内容 <select id="getRolesByUserId" resultType="java.lang.Stri ...
- shiro授权+注解式开发
shiro授权和注解式开发 1.shiro授权角色.权限 2.Shiro的注解式开发 ShiroUserMapper.xml <select id="getRolesByUserId& ...
- Shiro授权及注解式开发
目的: shiro授权 shiro注解式开发 Shiro授权 首先设计shiro权限表: 从图中我们也清晰的看出五张表之间的关系 ShiroUserMapper Set<String> g ...
随机推荐
- the essence of the internet idea
Computer Systems A Programmer's Perspective Second Edition Of course, we are glossing over many diff ...
- mysql基本sql语句大全(基础用语篇)
1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份 ...
- httpd.conf
修改配置文件-时会弹出一个文本式的文件 1.搜索:#LoadModule rewrite_module modules/mod_rewrite.so,去掉前面的# 2.全部替换AllowOverrid ...
- Jquery下拉效果
$('#触发元素').hover(function(){ $('#框框').slideDown(); //展开(动画效果)},function(){ $('#框框').slideUp(); //收起( ...
- 关于lnmp下搭thinkPHP无法找到指定静态页面
我在lnmp 下架了一个thinkPHP框架,非常奇怪,在环境都配置好后,我在url里输入localhost:10007/index.php/member/login,正常来说应该显示login.ht ...
- 【转】Http Cache最基本的一些东西
Http Cache最基本的一些东西 Cache浏览器IEwebkitApache Http的Cache机制总共有4个组成部分: Cache-Control: max-age=N(seconds) ...
- jq 拖拽
1.尼玛, move事件的时候忘了加ev,找了一个多小时 <!DOCTYPE html> <html> <head lang="en"> < ...
- placeholder兼容
<!------------placeholder兼容-------------><script type="text/javascript"> $( ...
- 微信公开课(北京站)速记 微信、微信支付、O2O的定义与关联
本文为4月29日微信公开课(北京站)微信产品部演讲全文速记,讲述了微信官方对微信.微信支付.O2O的定义与关联等问题的看法与观点. 作者:微信产品部 刘涵涛 吴毅 去年夏天有一个全民打飞机的盛况,这实 ...
- Android 脚本替换PackageName
原文简书地址:http://www.jianshu.com/p/dca9c323c686 1 前言 平时如果想要替换包名一般是在AS中右键Rename进行操作.但是如果遇到一份代码希望导出几种不同的包 ...