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代码的形式进行配置的,不过我觉着有点麻烦,就没进行说明,感兴趣的可以自行查找相关资料进行配置。

原文地址:https://www.cnblogs.com/SummerinShire/p/5746686.html