java注解式开发_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中进行引用。

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.config.BeanPostProcessor;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.ComponentScan;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.EnableAspectJAutoProxy;importorg.springframework.core.io.ClassPathResource;importorg.springframework.core.io.Resource;importorg.springframework.scheduling.annotation.EnableScheduling;importorg.springframework.util.FileCopyUtils;importjavax.servlet.Filter;importjavax.sql.DataSource;importjava.io.FileReader;importjava.io.IOException;

@Configuration

@EnableScheduling

@EnableAspectJAutoProxy

@ComponentScan(basePackages= {"com.lab.service", "com.lab.task", "com.lab.security"})public classApplicationConfig {

@AutowiredprivateDataSource dataSource;

@BeanpublicIDBI database() {

IDBI dbi= newDBI(dataSource);

logger.debug("dbi : {}", dbi);returndbi;

}//。。。

}

其次就是配置WebConfig文件:

同前一个一样,首先需要添加的就是@Configuration 标签,还有一个不同的就是需要加上@EnableWebMvc 标签以开启MVC模式。当然也有@ComponentScan 标签,使用方法同前,需要basePackages 属性的定义。按需也可添加诸如 @EnableAspectJAutoProxy 等标签。

这里定义的有譬如 setPrefix 和 setSuffix 等内容,具体按实际进行增减。

importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.ComponentScan;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.EnableAspectJAutoProxy;import org.springframework.web.servlet.config.annotation.*;importorg.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration

@EnableWebMvc

@EnableAspectJAutoProxy

@ComponentScan(basePackages= { "com.lab.controller"})public class WebConfig extendsWebMvcConfigurerAdapter {

@BeanpublicInternalResourceViewResolver viewResolver() {

InternalResourceViewResolver resolver= newInternalResourceViewResolver();

resolver.setPrefix("/WEB-INF/view/");

resolver.setSuffix(".jsp");returnresolver;

}

}

定义DataConfig文件:

前面我是直接在AppConfig中进行注入了数据库的定义,是因为具体的数据库连接的定义我是在这里进行的。

@Configuration 必不可少了,然后我又定义了一个属性 @Profile(“data”)用以说明此类的用途。然后就是定义了一个@Bean(name=“dataSource”) ,内容是数据库连接池、链接的用户名、密码、等待超时时间啦等等一系列的数据库的配置。

importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.context.annotation.Profile;importcom.alibaba.druid.pool.DruidDataSource;@Configuration

@Profile("data")public classDevConfig {

/* 此处我用了properties文件的方式进行数据库的定义,也可直接在代码中书写 */private static final String databaseConfig = "datasource.properties";

@Bean(name= "dataSource")publicDataSource dataSource() {

DruidDataSource dataSource= newDruidDataSource();

InputStream inStream= null;try{

Properties prop= newProperties();

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();}returndataSource;

}

}

这些都定义好后,在web.xml文件中进行配置下就行了。

contextConfigLocation

com.bell.lab.springconfig.DevConfig,

com.bell.lab.springconfig.ApplicationConfig

SpringDemoServlet

org.springframework.web.servlet.DispatcherServlet

contextClass

org.springframework.web.context.support.AnnotationConfigWebApplicationContext

contextConfigLocation

com.bell.lab.springconfig.WebConfig

1

SpringDemoServlet

/

spring.profiles.default

data

至此完事,跟在spring-application.xml中进行配置效果是一样的,整体上更符合java的习惯而已。

ps:其实,就算是web.xml文件,也可通过java代码的形式进行配置的,不过我觉着有点麻烦,就没进行说明,感兴趣的可以自行查找相关资料进行配置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值