Spring基础知识

利用spring完成松耦合

接口

public interface IOutputGenerator
{
public void generateOutput();
}

实现类

csv输出

public class CsvOutputGenerator implements IOutputGenerator
{
public void generateOutput(){
System.out.println("Csv Output Generator");
}
}

json输出

public class JsonOutputGenerator implements IOutputGenerator
{
public void generateOutput(){
System.out.println("Json Output Generator");
}
}

辅助类

public class OutputHelper
{
IOutputGenerator outputGenerator; public void generateOutput(){
outputGenerator.generateOutput();
} public void setOutputGenerator(IOutputGenerator outputGenerator){
this.outputGenerator = outputGenerator;
}
}

bean类配置文件

<!-- Spring-Common.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="OutputHelper" class="com.springtest.output.OutputHelper">
<property name="outputGenerator" ref="CsvOutputGenerator" />
</bean> <bean id="CsvOutputGenerator" class="com.springtest.output.impl.CsvOutputGenerator" />
<bean id="JsonOutputGenerator" class="com.springtest.output.impl.JsonOutputGenerator" /> </beans>

调用方式

public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"Spring-Common.xml"}); OutputHelper output = (OutputHelper)context.getBean("OutputHelper");
output.generateOutput(); }

结论

通过spring bean的方式进行配置可以灵活地将不同需求通过配置文件来完成,而不是改java原代码。通过面向接口的编程并结合spring的配置便可达到解耦的目的。

Spring JavaConfig实例

通常我们是通过xml的方式进行bean的声明

<bean id="helloBean" class="com.springtest.hello.impl.HelloWorldImpl">

使用 @Configuration 注释告诉 Spring,这是核心的 Spring 配置文件,并通过 @Bean 定义 bean。

@Configuration
public class AppConfig { @Bean(name="helloBean")
public HelloWorld helloWorld() {
return new HelloWorldImpl();
} }

Spring依赖注入(DI)

spring有两种注入的方式

  • setter方法注入
  • getter方法注入

setter方法注入

public class OutputHelper
{
IOutputGenerator outputGenerator; public void setOutputGenerator(IOutputGenerator outputGenerator){
this.outputGenerator = outputGenerator;
} }

配置文件

<bean id="OutputHelper" class="com.springtest.output.OutputHelper">
<property name="outputGenerator">
<ref bean="CsvOutputGenerator" />
</property>
</bean> <bean id="CsvOutputGenerator" class="com.springtest.output.impl.CsvOutputGenerator" />
<bean id="JsonOutputGenerator" class="com.springtest.output.impl.JsonOutputGenerator" />

构造函数注入

public class OutputHelper
{
IOutputGenerator outputGenerator; OutputHelper(IOutputGenerator outputGenerator){
this.outputGenerator = outputGenerator;
}
}

配置文件

<bean id="OutputHelper" class="com.springtest.output.OutputHelper">
<constructor-arg>
<bean class="com.springtest.output.impl.CsvOutputGenerator" />
</constructor-arg>
</bean> <bean id="CsvOutputGenerator" class="com.springtest.output.impl.CsvOutputGenerator" />
<bean id="JsonOutputGenerator" class="com.springtest.output.impl.JsonOutputGenerator" />

Spring Bean引用

Bean在不同的XML文件

<ref bean="someBean"/>

在同一个XML文件中的Bean

<ref local="someBean"/>

Spring bean属性值的注入

  • 正常的方式
  • 快捷方式
  • p模式

正常的方式

在一个value标签注入值,并附有property标签结束。

<bean id="FileNameGenerator" class="com.springtest.common.FileNameGenerator">
<property name="name">
<value>springtest</value>
</property>
<property name="type">
<value>txt</value>
</property>
</bean>

快捷方式

<property name="type" value="txt" />

<bean id="FileNameGenerator" class="com.springtest.common.FileNameGenerator">
<property name="name" value="springtest" />
<property name="type" value="txt" />
</bean>

p模式

<bean id="FileNameGenerator" class="com.springtest.common.FileNameGenerator"
p:name="springtest" p:type="txt" />

加载多个配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <import resource="common/Spring-Common.xml"/>
<import resource="connection/Spring-Connection.xml"/>
<import resource="moduleA/Spring-ModuleA.xml"/> </beans>

加载方式

ApplicationContext context =
new ClassPathXmlApplicationContext(Spring-All-Module.xml);

spring内部bean

一般的引用方式

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="CustomerBean" class="com.springtest.common.Customer">
<property name="person" ref="PersonBean" />
</bean> <bean id="PersonBean" class="com.springtest.common.Person">
<property name="name" value="springtest" />
<property name="address" value="address1" />
<property name="age" value="28" />
</bean> </beans>

如果一个bean仅仅是作为一个bean的内部bean。那么可以声明为内部bean。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="CustomerBean" class="com.springtest.common.Customer">
<property name="person">
<bean class="com.springtest.common.Person">
<property name="name" value="springtest" />
<property name="address" value="address1" />
<property name="age" value="28" />
</bean>
</property>
</bean>
</beans>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="CustomerBean" class="com.springtest.common.Customer">
<constructor-arg>
<bean class="com.springtest.common.Person">
<property name="name" value="springtest" />
<property name="address" value="address1" />
<property name="age" value="28" />
</bean>
</constructor-arg>
</bean>
</beans>

Spring Bean作用域

在Spring中,bean作用域用于确定哪种类型的 bean 实例应该从Spring容器中返回给调用者。bean支持的5种范围域:

  • 单例 - 每个Spring IoC 容器返回一个bean实例
  • 原型- 当每次请求时返回一个新的bean实例
  • 请求 - 返回每个HTTP请求的一个Bean实例
  • 会话 - 返回每个HTTP会话的一个bean实例
  • 全局会话- 返回全局HTTP会话的一个bean实例

在大多数情况下,可能只处理了 Spring 的核心作用域 - 单例和原型,默认作用域是单例

注:意味着只有在一个基于web的Spring ApplicationContext情形下有效!

单例

<bean id="customerService"
class="com.springtest.customer.services.CustomerService" />

原型

bean声明时添加scope="prototype"属性。

<bean id="customerService" class="com.springtest.customer.services.CustomerService"
scope="prototype"/>

此外还可以通过注解的方式来指定作用域。

Spring集合 (List,Set,Map,Properties)

spring支持4个主要的集合类型。

  • List – <list/>
  • Set – <set/>
  • Map – <map/>
  • Properties – <props/>

List注入方式

<property name="lists">
<list>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.springtest.common.Person">
<property name="name" value="springtestList" />
<property name="address" value="Hainan" />
<property name="age" value="28" />
</bean>
</list>
</property>

Set注入方式

<property name="sets">
<set>
<value>1</value>
<ref bean="PersonBean" />
<bean class="com.springtest.common.Person">
<property name="name" value="springtestSet" />
<property name="address" value="Hainan" />
<property name="age" value="28" />
</bean>
</set>
</property>

Map注入方式

<property name="maps">
<map>
<entry key="Key 1" value="1" />
<entry key="Key 2" value-ref="PersonBean" />
<entry key="Key 3">
<bean class="com.springtest.common.Person">
<property name="name" value="springtestMap" />
<property name="address" value="Hainan" />
<property name="age" value="28" />
</bean>
</entry>
</map>
</property>

Properties注入方式

<property name="pros">
<props>
<prop key="admin">admin@springtest.com</prop>
<prop key="support">support@springtest.com</prop>
</props>
</property>

Spring ListFactoryBean 创建一个具体的列表集合类(ArrayList和LinkedList)

方式一

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="CustomerBean" class="com.springtest.common.Customer">
<property name="lists">
<bean class="org.springframework.beans.factory.config.ListFactoryBean">
<property name="targetListClass">
<value>java.util.ArrayList</value>
</property>
<property name="sourceList">
<list>
<value>one</value>
<value>2</value>
<value>three</value>
</list>
</property>
</bean>
</property>
</bean> </beans>

方式二

通过util:list</util:list>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <bean id="CustomerBean" class="com.springtest.common.Customer">
<property name="lists">
<util:list list-class="java.util.ArrayList">
<value>one</value>
<value>2</value>
<value>three</value>
</util:list>
</property>
</bean> </beans>

Spring SetFactoryBean创建一个具体的Set集合(HashSet 和 TreeSet)

方式一

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="CustomerBean" class="com.springtest.common.Customer">
<property name="sets">
<bean class="org.springframework.beans.factory.config.SetFactoryBean">
<property name="targetSetClass">
<value>java.util.HashSet</value>
</property>
<property name="sourceSet">
<list>
<value>one</value>
<value>2</value>
<value>three</value>
</list>
</property>
</bean>
</property>
</bean> </beans>

方式二

通过<util:set></util:set>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <bean id="CustomerBean" class="com.springtest.common.Customer">
<property name="sets">
<util:set set-class="java.util.HashSet">
<value>one</value>
<value>2</value>
<value>three</value>
</util:set>
</property>
</bean> </beans>

Spring MapFactoryBean创建一个具体的Map集合类(HashMap和TreeMap)

方式一

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="CustomerBean" class="com.springtest.common.Customer">
<property name="maps">
<bean class="org.springframework.beans.factory.config.MapFactoryBean">
<property name="targetMapClass">
<value>java.util.HashMap</value>
</property>
<property name="sourceMap">
<map>
<entry key="Key1" value="one" />
<entry key="Key2" value="two" />
<entry key="Key3" value="three" />
</map>
</property>
</bean>
</property>
</bean> </beans>

方式二

通过<util:map></util:map>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <bean id="CustomerBean" class="com.springtest.common.Customer">
<property name="maps">
<util:map map-class="java.util.HashMap">
<entry key="Key1" value="1" />
<entry key="Key2" value="2" />
<entry key="Key3" value="3" />
</util:map>
</property>
</bean> </beans>

Spring PropertyPlaceholderConfigurer数据库配置

pom.xml中添加依赖

<!-- mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency> <!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.20</version>
</dependency>

属性配置文件:system-config.properties

################################################
# DataSource Config
jdbc.url=jdbc\:mysql\://127.0.0.1:3306/quartz?useUnicode\=true&characterEncoding\=utf8&autoReconnect\=true&useSSL\=false&zeroDateTimeBehavior\=convertToNull
jdbc.username=root
jdbc.publicKey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALIT9sFn8U+Hoo80Q+Hepwc0ZN6HBiyAW4SiLCXLhNNjxB45mtRamABoB0O9dEsziT/gwtuXMuC2bWePdCvEb1ECAwEAAQ==
jdbc.password=fCHxOiDBDsWY/BJLg05fbNGvQmDRPZJufcvyqCqml+zwmB4Gw/Bn7lzy8w117CQ1jEBFpj0ERgQsCBJD0ROfJw==

applicationContext.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 只需要加载service、dao即可,不需要加载controller -->
<context:component-scan base-package="com.quartz">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan> <!-- 占位符 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:system-config.properties</value>
</list>
</property>
</bean> <!-- 防SQL注入过滤器 -->
<bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
<property name="dbType" value="mysql" />
</bean>
<!-- 监控信息过滤器 -->
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
<!-- slowSqlMillis用来配置SQL慢的标准,执行时间超过slowSqlMillis的就是慢。 -->
<property name="slowSqlMillis" value="10000" />
<property name="logSlowSql" value="true" />
<property name="mergeSql" value="true" />
</bean> <!-- 数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<!-- 密码加密 -->
<property name="filters" value="config" />
<property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${jdbc.publicKey}" />
<property name="password" value="${jdbc.password}" /> <!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="5" />
<property name="minIdle" value="5" />
<property name="maxActive" value="30" /> <!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="false" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 超过时间限制是否回收 -->
<property name="removeAbandoned" value="true" />
<!-- 超时时间;单位为秒。180秒=3分钟 -->
<property name="removeAbandonedTimeout" value="180" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" />
<property name="proxyFilters">
<list>
<!-- 监控信息过滤器 -->
<ref bean="stat-filter" />
<!-- 防注入的话从前台传排序字段排序不好用 -->
<ref bean="wall-filter" />
</list>
</property>
</bean> </beans>

Spring bean配置继承

在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置。可通过parent属性进行配置。

一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性。另外,子 Bean 允许覆盖继承的值。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="BaseCustomerMalaysia" class="com.springtest.common.Customer">
<property name="country" value="Malaysia" />
</bean> <bean id="CustomerBean" parent="BaseCustomerMalaysia">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean> </beans>

继承抽象

如果你要让这个 bean 作为一个基础模板,不允许别人来实例化它,可以在一个元素中添加一个abstract的属性。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="BaseCustomerMalaysia" class="com.springtest.common.Customer" abstract="true">
<property name="country" value="Malaysia" />
</bean> <bean id="CustomerBean" parent="BaseCustomerMalaysia">
<property name="action" value="buy" />
<property name="type" value="1" />
</bean> </beans>

如果想实例化BaseCustomerMalaysia则会报错。

纯继承模板和覆盖

父 bean 是不需要定义类的属性,很多时候,你可能只需要一个共同的属性共享。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="BaseCustomerMalaysia" abstract="true">
<property name="country" value="Malaysia" />
</bean> <bean id="CustomerBean" parent="BaseCustomerMalaysia"
class="com.springtest.common.Customer">
<property name="country" value="Japan" /> <!--覆盖-->
<property name="action" value="buy" />
<property name="type" value="1" />
</bean> </beans>

Spring依赖检查

在Spring中,可以使用依赖检查功能,以确保所要求的属性可设置或者注入。

依赖检查模式

4个依赖检查支持的模式:

  • none – 没有依赖检查,这是默认的模式。
  • simple – 如果基本类型(int, long,double…)和集合类型(map, list..)的任何属性都没有设置,UnsatisfiedDependencyException将被抛出。
  • objects – 如果对象类型的任何属性都没有设置,UnsatisfiedDependencyException将被抛出。
  • all – 如果任何类型的任何属性都没有被设置,UnsatisfiedDependencyException将被抛出。

注:默认模式是 none

dependency-check="simple"

<bean id="CustomerBean" class="com.springtest.common.Customer"
dependency-check="simple"> <property name="person" ref="PersonBean" />
<property name="action" value="buy" />
</bean>

Spring 特定属性依赖检查

通过dependency-check的方式进行依赖检查是对整个类型的属性都进行了检查。然而大多数情况下只需要对某个属性进行检查即可。因此引入了@Required注解对某个属性进行检查。

配置文件设置

方式一

添加 Spring 上下文和 <context:annotation-config />在bean配置文件。

<beans
...
xmlns:context="http://www.springframework.org/schema/context"
...
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
...
<context:annotation-config />
...
</beans>

方式二

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

** bean 设置 **

在对应属性的set方法上添加@Required

@Required
public void setPerson(Person person) {
this.person = person;
}

Spring自定义@Required-style注解

创建注解接口Mandatory(名称可以自己起)

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Mandatory {
}

注解注册

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor">
<property name="requiredAnnotationType" value="com.springtest.common.Mandatory"/>
</bean>

注解引用

@Mandatory
public void setPerson(Person person) {
this.person = person;
}

至此,创建了一个新的自定义命名 @Required-style的@Mandatory 注解,相当于 @Required 注解。

spring bean初始化准备和销毁清理

方式一:

Spring Bean InitializingBeanDisposableBean

  • 如果bean实现了InitializingBean,则afterPropertiesSet()会在所有的 bean 属性设置后执行。
  • 如果bean实现了DisposableBean,则destroy()会在Spring容器释放后执行。

注意:但不建议这样使用。因为这样增加了代码与spring的耦合度。

方式二

因此建议使用init-methoddestroy-method

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.springtest.customer.services.CustomerService"
init-method="initIt" destroy-method="cleanUp"> <property name="message" value="i'm property message" />
</bean> </beans>

方式三

使用@PostConstruct@PreDestroy 注解

  • 注:@PostConstruct@PreDestroy 标注不属于 Spring,它是在J2EE库- common-annotations.jar

添加注解扫描

通过CommonAnnotationBeanPostProcessor的方式

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

通过<context:annotation-config />的方式

<context:annotation-config />

注解应用

@PostConstruct
public void initIt() throws Exception {
System.out.println("Init method after properties are set : " + message);
} @PreDestroy
public void cleanUp() throws Exception {
System.out.println("Spring Container is destroy! Customer clean up");
}

spring el表达式

spring 自动扫描组件

  • @Entity实体bean
  • @Service用于标注业务层组件
  • @Controller用于标注控制层组件(如struts中的action)
  • @Repository用于标注数据访问组件,即DAO组件
  • @Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

注册扫描组件

<context:component-scan base-package="com.springtest.customer" />

Spring过滤器组件自动扫描

添加过滤器

不包含过滤器

<!-- 只需要加载service、dao即可,不需要加载controller -->
<context:component-scan base-package="com.springtest">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>

一般结合spring mvc自动扫描组件一起用

<!-- 扫描所有controller组件 -->
<context:component-scan base-package="com.quartz">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan> <!--注册requestmapping,否则无法扫描@RequestMapping的注解-->
<mvc:annotation-driven />

包含过滤器

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.springtest" > <context:include-filter type="regex"
expression="com.springtest.customer.dao.*DAO.*" /> <context:include-filter type="regex"
expression="com.springtest.customer.services.*Service.*" /> </context:component-scan> </beans>

Spring自动注入Beans实体

通过autowire(此种方式基本不用了)属性和@Autowired (一般采用这种方式进行处理)注解

autowire

autowire提供5种装配方式。

  • no – 缺省情况下,自动配置是通过“ref”属性手动设定
  • byName – 根据属性名称自动装配。如果一个bean的名称和其他bean属性的名称是一样的,将会自装配它。
  • byType – 按数据类型自动装配。如果一个bean的数据类型是用其它bean属性的数据类型,兼容并自动装配它。
  • constructor – 在构造函数参数的byType方式。
  • autodetect – 如果找到默认的构造函数,使用“自动装配用构造”; 否则,使用“按类型自动装配”。
<bean id="customer" class="com.springtest.common.Customer" autowire="byName" />

@Autowired

  • 建议采用此种方式。

注册AutowiredAnnotationBeanPostProcessor

方式1:

<context:annotation-config />

方式2:

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

使用

@Autowired
private Person person;

Spring AOP切面(Advice)编程

可以有多种方式,其中面向方法的切面可参考,Spring AOP基于配置文件的面向方法的切面

jdbc操作

最新文章

  1. 【Django】--Model字段
  2. 数据库整合数据报表SQL实战
  3. sql 将查询结果为多行一列合并为一行一列
  4. 更改SAP的字段翻译
  5. [原创]android开源项目源码解析(一)----CircleImageView的源码解析
  6. C++-const_cast只能用于指针和引用,对象的const到非const可以用static_cast
  7. Qt之显示网络图片(可以改成升级模块)
  8. Floyd-Warshall算法的理解
  9. c#开发之多国语言解决方案gnu.gettext + poedit
  10. 【集美大学1411_助教博客】个人作业2——英语学习APP案例分析 成绩
  11. 文本分类学习(三) 特征权重(TF/IDF)和特征提取
  12. 在js中获取上传图片的宽度和高度
  13. Eclipse远程调试+FTPClient在jdk6以上写法不兼容问题的排查
  14. 九、文件IO——案例构建标准库
  15. sendfile
  16. BZOJ2553 [BeiJing2011]禁忌 AC自动机 矩阵
  17. EF Core 2.1变化
  18. oracle 12c中种子数据库的隐藏与保护
  19. ElementUI(vue UI库)、iView(vue UI库)、ant design(react UI库)中组件的区别
  20. 查看Oracle数据库中的,已经连接好的..当前用户状况

热门文章

  1. BZOJ 1857 传送带 (三分套三分)
  2. ModernUI教程:如何使用你自己的导航框架
  3. C#如何防止程序多次运行的技巧
  4. JavaScript的理解记录(6)
  5. HTML+CSS知识点总结
  6. JUnit备忘录
  7. bzoj 1305 dance跳舞
  8. BZOJ 4568 幸运数字
  9. backup mysql
  10. js整数补零