<context:annotation-config> 和 <context:component-scan>的区别
<context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解。
<context:component-scan>除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的package下扫描以及注册javabean 。
2.关于spring 3.0.5的<mvc:resources> 标签的使用
spring mvc 的<mvc;resources mapping="***" location="***">标签是在spring3.0.4出现的,主要是用来进行静态资源的访问。
在spring3.0.4出来的时候spring还没有更新其schema所以在配置文件中有可能找不到<mvc:resources >标签,这个问题在spring3.0.5中已经解决,
而且网上也有很多其他的解决方案,我在这里就不记录了。
首先使用spring mvc需要配置其使用的servlet.在web.xml中:
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
这里给 servlet-name定义的名称是springMVC,这样的话会在web-inf下spring会自动扫描一个XML文件名叫springMVC-servlet.xml文件,这里都是spring自动扫描的,如果你没有提供,将会报一个文件查找不到的异常。
看了下org.springframework.web.servlet.DispatcherServlet加载这个文件的过程,貌似这个文件存放的地址也是可以进行设置的,具体怎么搞我还没有研究。
由于spring mvc拦截了所有请求,所以当你设置
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
的时候会影响到静态资源文件的获取,这样就需要有这个标签来帮你分类完成获取静态资源的责任。
springMVC-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:resources mapping="/javascript/**" location="/static_resources/javascript/"/>
<mvc:resources mapping="/styles/**" location="/static_resources/css/"/>
<mvc:resources mapping="/images/**" location="/static_resources/images/"/>
<mvc:default-servlet-handler />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
这里可以看到我所有的页面引用到/styles/**的资源都从/static_resources/css里面进行查找。
页面的一段静态资源访问的代码。
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<HTML>
<HEAD>
<TITLE> ABCDEFG </TITLE>
<link type="text/css" rel="stylesheet" href="<c:url value='/styles/siteboard.css'/>">
...
...
...
可能这个标签的真谛就是为了引用资源的访问不会类似CONTROLLER一样被拦截,区分出关注的资源的访问,一般我们在springMVC里面的拦截都会配置为"/",拦截所有的。
3. Spring注解配置定时任务<task:annotation-driven/>
首先在配置文件头部的必须要有:
xmlns:task="http://www.springframework.org/schema/task"
其次xsi:schemaLocation必须为其添加:
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
然后spring扫描过程必须涵盖定时任务类所在的目录:
<context:component-scan base-package="com.xx.xx" />
com.xx.xx属于定时任务类的父级甚至更高级
然后设置动作启用定时任务
<task:annotation-driven/>
最后设置任务类
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@Lazy(value=false)
public class MyQuartzs {
@Scheduled(cron = "*/5 * * * * ?")//每隔5秒执行一次
public void test() throws Exception {
System.out.println("Test is working......");
}
//@Scheduled(cron = "0 0 1 * * ?")//每天凌晨1点整
//@Scheduled(cron = "0 30 0 * * ?")//每天凌晨0点30分
//@Scheduled(cron = "0 */60 * * * ?")//1小时处理一次
}
4.spring循环引用的问题
Bean with name ‘xxxService’ has been injected into other beans [xxxService] in its raw version as part of a circular reference, but has eventually been wrapped.
This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching – consider using ‘getBeanNamesOfType’
with the ‘allowEagerInit’ flag turned off, for example.
用的spring版本是2.5.6,之前也有遇到过这种问题,确实是代码设计得比较耦合,将相关逻辑拆分后得到解决。这两天这个问题,不管我如何拆解都如影随形,
已经不是简单的A->B,B->A的循环引用了,而是深层次的逻辑耦合,要解耦比较困难,说明在设计阶段有提高的余地。诡异的是,在融入另一个项目前是不会抛这个错误的,
可见问题可能出在和新项目融合后的配置文件上,简单分析了一下还是不得要领,于是开始启用google了。Juergen Hoeller说:
This is probably a consequence of the bean initialization order having changed, in combination with auto-proxying and maybe a pointcut that is too broad.
然后他给出了解决方案,有一个参数setAllowRawInjectionDespiteWrapping,默认是false,将其设成true即可。代码如下:
<span style="font-size:18px">public class MyWebApplicationContext extends XmlWebApplicationContext {
@Override
protected DefaultListableBeanFactory createBeanFactory() {
DefaultListableBeanFactory beanFactory = super.createBeanFactory();
beanFactory.setAllowRawInjectionDespiteWrapping(true);
return beanFactory;
}
}</span>
然后在web.xml配置启用此context,
<span style="font-size:18px"><context-param>
<param-name>contextClass</param-name>
<param-value>xxx.MyWebApplicationContext</param-value>
</context-param></span>
5.Spring的PropertyPlaceholderConfigurer应用
Spring 利用PropertyPlaceholderConfigurer占位符
1). PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。
PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去.
在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进行修改,而不用对xml配置文件进行修改。
2).在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码,如:
<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>conf/sqlmap/jdbc.properties</value>
</property>
<property name="fileEncoding">
<value>UTF-8</value>
</property>
</bean>
当然也可以引入多个属性文件,如:
<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/mail.properties</value>
<value>classpath: conf/sqlmap/jdbc.properties</value>//注意这两种value值的写法
</list>
</property>
</bean>
3).譬如,jdbc.properties的内容为:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mysqldb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=round;
jdbc.username=root
jdbc.password=123456
4).那么在spring配置文件中,我们就可以这样写:
<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath: conf/sqlmap/jdbc.properties </value>
</list>
</property>
</bean>
<bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
<property name="driverClassName"value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}"/>
<property name="password"value="${jdbc.password}" />
</bean>
5).这样,一个简单的数据源就设置完毕了。可以看出:PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。
6).查看源代码,可以发现,locations属性定义在PropertyPlaceholderConfigurer的祖父类 PropertiesLoaderSupport中,而location只有 setter方法。类似于这样的配置,在spring的源程序中很常见的。
PropertyPlaceholderConfigurer如果在指定的Properties文件中找不到你想使用的属性,它还会在Java的System类属性中查找。
我们可以通过System.setProperty(key, value)或者java中通过-Dnamevalue来给Spring配置文件传递参数。
6. Maven中Pom文件打包配置build
方法一
<build>
<finalName>###架包名###</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<!-- 控制资源文件的拷贝 -->
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}</targetPath>
<!-- excludes和includes二选一使用即可 -->
<!-- 不包含的文件,支持通配符 -->
<excludes>
<exclude>*.txt</exclude>
</excludes>
<!-- 包含的文件,支持通配符 -->
<includes>
<include>*.properties</include>
</includes>
</resource>
</resources>
<plugins>
<!-- 设置源文件编码方式 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.eya.main.Hello</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- 拷贝依赖的jar包到lib目录 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 解决资源文件的编码问题 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 打包source文件为jar文件(源码,可选) -->
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.1</version>
<configuration>
<attach>true</attach>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>wuxiA</id>
<build>
<filters>
<filter>路径/application-环境名.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>wuxiB</id>
<build>
<filters>
<filter>路径/application-环境名.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>dev</id>
<build>
<filters>
<filter>路径/application-环境名.properties</filter>
</filters>
</build>
</profile>
</profiles>
方法二:
<build>
<resources>
<resource>
<targetPath>${project.build.directory}/classes</targetPath>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.test.testguava.app.App</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>applicationContext.xml</resource>
</transformer>
</transformers>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>executable</shadedClassifierName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>wuxiA</id>
<build>
<filters>
<filter>路径/application-环境名.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>wuxiB</id>
<build>
<filters>
<filter>路径/application-环境名.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>dev</id>
<build>
<filters>
<filter>路径/application-环境名.properties</filter>
</filters>
</build>
</profile>
</profiles>