springmvc注入service 属性必须注入吗

以前总是在弄Struts+Spring+jdbc、Ibatis实现MVC,最近公司项目换Spring mvc实现方式,从网络上找了一个项目来练手,现在总结一下,mvc无非就是分清M V C 各自功能,而web程序无非就是要解决web容器数据和java程序数据传输问题(就程序开发而言)。如果你理解MVC, 对SSH有基本的了解,学习Spring MVC 就可以套用SSH的基本实现。 在struts中使用 ActionServlet作为请求分发器,依据请求路径信息将请求分派到各个action中,调用基本数据CURD方法。在SSH中,ActionServlet作为Control , JSP/HTML作为View承担着视图功能,Action作为mode和struts的“适配器”,将请求数据转发给后端的DAO处理。在依据处理结果,跳转到对应的View,其中ActionMapping是对View的抽象。 有了这个基本认识,我们类比类推,spring mvc,在spring mvc中比较重要的两个包就是spring-web.jar spring-context.jar、基本的ioc等jar包。 org.springframework.web.servlet.DispatcherServlet作为Control ,它做的事和ActionServlet差不多。JSP/HTML作为View,在spring最大的亮点莫过于和mode、dao交互的类可以是普通的JAVABEAN,在struts中的action到了spring 中就完全变成了POJO。和Struts2 的Aciton一样,减少了数据侵入性。下面我们就用注解实现Spring mvc。
我们用注解方式实现spring
mvc,下面我们要对基本的产使用的注解有个基本的理解,注解的存在使得我们可以减少spirng的xml配置,做的事情和xml配置差不多。所以我们需要的注解就包括三大类:
* bean 属性注入注解,例如 @Autowired 、@Resource
* bean方法执行注解,例如 @PostConstruct 、@PreDesotory
* bean 定义注解,例如@Component
、 @Control 、@Service 、 @Repository等。
@Autowired、@Resource作为常使用的属性注入注解,区别在于:
1、@Autowired
是依据type来自动封装注入的;而@Resource 先按照name,在按照type依赖注入
2、扫描处理注解的处理类不同,要让@Autowired 生效,必须在spring xml配置文件中加入
&bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /&
而要让@Resource 生效,必须在spring xml配置文件中加入
&context:annotation-config /&
通过&context:annotation-config /& 将隐式的向容器注入AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor。
使用Autowired当IOC中拥有两个相同目标类型时候,可以使用@Qualifier指定具体使用那个类,下面代码指定id为userDao的类注入进来:
@Autowired
@Qualifier("userDao")
当IOC容器找不到 Autowired指定的类型时,可以通过如下方式配置,这样就需要在代码中做null判断:
@Autowired(required= false)
public void setUserDao(UserDao userDao){
this.userDao = userD
@PostConstruct 、 @PreDestory 用于定义方法在对象是实例化后执行的方法、和在对象销废之前调用的方法。
例如子类需要实例化父类的变量,就可以再对应的方法中添加@PostConstruct、某些类在内存中销废之前需要释放自己的资源可以使用@PreDestory注解
@Component 、 @Control 、 @Service 、 @Repository 用于向IOC容器中注入bean:
@Component
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
如上代码会在ioc容器中声明id名称为userDaoImpl的bean,如果指定了名称,如@Component("userDao"),那就会在ico容器中声明id为userDao的bean,没有添加参数默认使用的就是非限定类名的骆驼命名法方式定义bean。其他@Control、@Service、@Repository和@Component的作用一样,但他们更加答意,表示控制层、业务层、持久层。
要让上述bean定义注解生效需要在spring配置文件中添加如下xml配置:
&context:component-scan base-package="com.kedacom.ksoa" /&
这个处理类会将包下所有添加了注解的类实例化并且注入到IOC。加入类中添加了@Resource、@Autowired等属性注入注解,该类也会一并处理,不需要在添加&context: annocation-config /&配置了。base-page表示需要扫描的包,base-package指定的类包及其递归子包中所有的类都会被处理。
&context:component-scan /&还允许定义过滤器将基包下的某些类纳入或排除。Spring支持以下4种类型的过滤方式:
过滤器类型 表达式范例 说明
注解 org.example.SomeAnnotation 将所有使用SomeAnnotation注解的类过滤出来
类名指定 org.example.SomeClass 过滤指定的类
正则表达式 com\.kedacom\.spring\.annotation\.web\..* 通过正则表达式过滤一些类
AspectJ表达式 org.example..*Service+ 通过AspectJ表达式过滤一些类
值得注意的是&context:component-scan /&配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此当使用&context:component-scan /&后,就可以将&context:annotation-config /&移除了。
使用@Scope来定义Bean的作用范围 在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完成这项工作:
@Scope("session")
@Component()
public class UserSessionBean implements Serializable {
3. 参考 /blog/244002 /topic/244153 http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-annotation-config http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-classpath-scanning
浏览 14179
浏览: 146576 次
来自: 深圳
在windows下可以转MP3,然而在linux下转的MP3都 ...
楼主辛苦了, 问下其它音频格式的参数如何获取呀, 如amr,
gif分解成jpg后,图片的色相不对了,偏红,求解
public static String toMPTree(S ...
謝謝 交作業.8066人阅读
spring(8)
& & & & & 上一篇博客,学习了,即利用spring容器来为类中的属性赋值,分为两种赋值方法,利用set和利用构造方法,我们都知道,如果我需要为某一个属性赋值的话,必须为该属性写上set方法,那么大家有没有想过一个问题,如果我们一个类中有很多个属性,我们会生成大量的set方法,如果用构造方法来赋值,即&constructor-arg
index=&& type=&& ref=&& value=&&&&/constructor-arg&,这样也会存在很多个这样的标签,因为一个&constructor-arg index=&& type=&& ref=&& value=&&&&/constructor-arg&只能为一个属性赋值,为了解决这些难题,我们可以利用注解来为属性进行赋值。
& & & &同样,我们新建两个类ClassInfo.java和Teacher.java
package com.test.spring.
public class ClassInfo {
public void printClassInfo() {
System.out.println(&this is classInfo....&);
package com.test.spring.
import javax.annotation.R
public class Teacher {
private String teacherN
private ClassInfo classI
public void printClassInfo() {
this.classInfo.printClassInfo();
& & 注意,这里我们给需要被赋值的属性上添加了@Resource的注解。其中包含了基本的String类型以及ClassInfo的引用类型。接下来我们来写一个applicationContext.xml文件:
注意:因为我们使用注解来为属性赋值的,所以我们需要导入命名空间,如下:
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&&/context:annotation-config&
3.导入需要被spring容器来管理的类。
& & & &经过上面散步操作之后呢,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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
&context:annotation-config&&/context:annotation-config&
&bean id=&classInfo& class=&com.test.spring.di.ClassInfo&&&/bean&
&bean id=&teacher& class=&com.test.spring.di.Teacher&&&/bean&
&/beans&此时,编写我们的测试类TestAnotation.java
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(&com/test/spring/di/applicationContext.xml&);
Teacher teacher = (Teacher) applicationContext.getBean(&teacher&);
teacher.printClassInfo();此时打印如下:
this is classInfo....
这充分说明了利用注解为Teacher类中的classInfo对象赋值成功了。在这里需要注意:如果我么你的@Resource的注解的值为&&,那么spring将会将容器中id为该属性的类注入给该属性。什么意思呢??我举个栗子:
我将上面的@Resource的值修改为这样:@Resource(name=&test&)此时由于name的值不是&&,所以spring容器将不会试图匹配与该属性相同的id对应的类,而是将id为test的类注入给该属性,然而,此时并没有id为test的类,所以会抛出如下异常:
No bean named 'test' is defined
此时我将classInfo对应的id改为test,再次运行发现程序是ok的。
& & & 其实spring有自己定义的注解的,细心的程序员可能发现了@Resource是java官方提供的一个注解,并不是spring的,我们可以将@Resource替换成@Autowired,这个@Autowired是根据类型进行匹配的。那我如果非要按照id来进行匹配怎么办呢?别急,spring为我们提供了一个@Qualifier,我们如果要匹配spring容器中id为classInfo的类,可以这样写:@Qualifier(&classInfo&),注意:这里还需要加上@Autowired,如下:
@Autowired
@Qualifier(&classInfo&)
private ClassInfo classI
& & & & 大家有没有想过,既然spring是用来管理bean的,难道就没有生命周期的管理???是有的。我们为ClassInfo类添加如下初始化和销毁方法:
@PostConstruct
public void init() {
System.out.println(&classInfo init....&);
@PreDestroy
public void destroy() {
System.out.println(&classInfo destroy.....&);
}顾名思义,@PostConstruct表示紧接着在构造方法之后执行的,@PreDestroy表示在销毁前执行的,然后编写测试代码如下:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(&com/test/spring/di/applicationContext.xml&);
ClassPathXmlApplicationContext classContext = (ClassPathXmlApplicationContext) applicationC
Teacher teacher = (Teacher) classContext.getBean(&teacher&);
teacher.printClassInfo();
classContext.close();此时运行工程,会打印如下信息:
classInfo init....
this is classInfo....
classInfo destroy.....
我们发现确实执行了初始化和销毁的方法,说明利用注解来实现声明周期的管理也是可以的。
& & & 但是,注意spring的注解只能用于引用类型。&
& & & 那我能不能在applicationContext.xml中对于bean的声明也不写呢??是可以的,怎么做的,同样分为以下三步:
1.导入命名空间:
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&
2.启动扫描类的注解解析器
&context:annotation-config&&/context:annotation-config&
3.启动依赖注入的注解解析器
&context:component-scan base-package=&com.test.spring.di&&&/context:component-scan&
这里是扫描&com.test.spring.di&包和该包下的所有的类。
4.在需要被spring来管理的类上加上@Component的注解
注意:和@Resource比较相似,@Component是默认匹配类名的第一个字母小写的,比如我在Teacher类上加了@Component这个注解,那么可以这样得到该类对象:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(&com/test/spring/di/applicationContext.xml&);
Teacher teacher = &(Teacher) applicationContext.getBean(&teacher&);
如果Teacher类上写了这样一个@Component(&teacherId&),那么此时就需要通过
Teacher teacher = &(Teacher) applicationContext.getBean(&teacherId&);
才可以得到Teacher对象。这里的@Component是一个比较宽泛的泛型,spring为我们提供了更详细的注解配置:
@Controller &配置控制器的,控制视图的跳转 &
@Service & & 配置service的,不是android中的service
@Repository & &配置dao的,控制操作数据库的
& & &我们发现这三个注解是对web开发中的MVC编程很好的一个支持。
& & & 恩,今天spring注解,就到这里吧,希望大家看了都能理解。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:350159次
积分:6367
积分:6367
排名:第4027名
原创:232篇
评论:86条
文章:22篇
阅读:40789
文章:11篇
阅读:27868
阅读:22425
(2)(4)(3)(5)(2)(8)(5)(16)(6)(4)(2)(2)(4)(11)(12)(2)(8)(5)(9)(7)(19)(16)(36)(4)(1)(7)(4)(22)(8)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'&&(尊重劳动成果,转载请注明出处:冷血之心的博客)&spring是开源的轻量级框架,核心主要两部分:(1)aop:面向切面编程,扩展功能不是修改源代码实现(2)ioc:控制反转,- 比如有一个类,在类里面有方法(不是静态的方法),调用类里面的方法,创建类的对象,使用对象调用方法,创建类对象的过程,需要new出来对象- 把对象的创建不是通过new方式实现,而是交给spring配置创建类对象&spring也是一站式框架(1)spring在javaee三层结构中,每一层都提供不同的解决技术- web层:springMVC- service层:spring的ioc- dao层:spring的jdbcTemplate&Spring中对bean标签的管理:Spring里边是通过配置文件来创建对象的,即其有IOC控制反转机制,每一个bean的实例化就是在创建一个对象。bean实例化有三种实现方式:(1)使用类的无参构造函数来创建(重点)比如说有一个User类:package cn.itcast.
public class User {
public User() {
则只需要在核心配置文件中:&bean id=&user& class=&cn.ywq.User& &&/bean& 即可完成对象的创建(bean的实例化),若User类中没有无参构造,则会报异常。&(2)使用静态工厂创建:创建静态的方法,返回类对象package cn.ywq.
public class Bean2Factory {
//静态的方法,返回Bean2对象
public static Bean2 getBean2() {
return new Bean2();
}&bean id=&bean2& class=&cn.ywq.bean.Bean2Factory& factory-method=&getBean2&&&/bean&(3)使用实例工厂创建创建不是静态的方法,返回类对象 npackage cn.ywq.
public class Bean3Factory {
//普通的方法,返回Bean3对象
public Bean3 getBean3() {
return new Bean3();
}&bean id=&bean3Factory& class=&cn.ywq.bean.Bean3Factory&&&/bean&
&bean id=&bean3& factory-bean=&bean3Factory& factory-method=&getBean3&&&/bean&Bean标签的常用属性:(1)id属性:起名称,id属性值名称任意命名- id属性值,不能包含特殊符号- 根据id值得到配置对象(2)class属性:创建对象所在类的全路径(3)name属性:功能和id属性一样的,id属性值不能包含特殊符号,但是在name属性值里面可以包含特殊符号(4)scope属性- singleton:默认值,单例- prototype:多例&- request:创建对象把对象放到request域里面- session:创建对象把对象放到session域里面- globalSession:创建对象把对象放到globalSession里面&&属性注入:上边介绍了通过配置文件,有三种方法可以实现对象的创建,那么如何向对象的属性中设置值呢?这就需要用到注入的方法。属性注入的方式介绍(三种方式)(1)使用set方法注入(2)使用有参数构造注入(3)使用接口注入在spring框架里面,支持前两种方式(1)set方法注入(重点)(2)有参数构造注入&有参构造注入:package cn.ywq.
public class PropertyDemo1 {
public PropertyDemo1(String username) {
this.username =
public void test1() {
System.out.println(&demo1..........&+username);
}配置文件:&!-- 使用有参数构造注入属性 --&
&bean id=&demo& class=&cn.itcast.property.PropertyDemo1&&
&!-- 使用有参构造注入 --&
&constructor-arg name=&username& value=&小王小马&&&/constructor-arg&
使用set方法注入属性(重点):package cn.ywq.
public class Book {
public void setBookname(String bookname) {
this.bookname =
public void demobook() {
System.out.println(&book...........&+bookname);
}配置文件:&!-- 使用set方法注入属性 --&
&bean id=&book& class=&cn.itcast.property.Book&&
&!-- 注入属性值
name属性值:类里面定义的属性名称
value属性:设置具体的值
&property name=&bookname& value=&易筋经&&&/property&
&/bean& &前面属性注入都是将简单的字符串作为属性注入到了某个对象中,那么如何注入对象类型的属性呢?(重点)我们以Service中得到Dao对象为例来进行演示。创建service类和dao类:(1)在service得到dao对象具体实现过程:(1)在service里面把dao作为类型属性(2)生成dao类型属性的set方法UserService.javapackage cn.ywq.
public class UserService {
//1 定义dao类型属性
private UserDao userD
//2 生成set方法
public void setUserDao(UserDao userDao) {
this.userDao = userD
public void add() {
System.out.println(&service.........&);
userDao.add();
UserDao.javapackage cn.ywq.
public class UserDao {
public void add() {
System.out.println(&dao.........&);
配置文件:&!-- 注入对象类型属性 --&
&!-- 1 配置service和dao对象 --&
&bean id=&userDao& class=&cn.ywq.ioc.UserDao&&&/bean&
&&bean id=&userService& class=&cn.ywq.ioc.UserService&&
注入dao对象
name属性值:service类里面属性名称
现在不要写value属性,因为刚才是字符串,现在是对象
写ref属性:dao配置bean标签中id值 --&
&property name=&userDao& ref=&userDao&&&/property&
&/bean& &注入复杂类型属性如何注入复杂类型属性呢?复杂类型包括:数组、list集合、map集合以及properties类型。举例:在User对象中注入各种复杂类型属性。&!-- 注入复杂类型属性值 --&
&bean id=&user& class=&cn.ywq.property.User&&
&!-- 数组 --&
&property name=&arrs&&
&value&小王&/value&
&value&小马&/value&
&value&小宋&/value&
&/property&
&!-- list --&
&property name=&list&&
&value&小奥&/value&
&value&小金&/value&
&value&小普&/value&
&/property&
&!-- map --&
&property name=&map&&
&entry key=&aa& value=&lucy&&&/entry&
&entry key=&bb& value=&mary&&&/entry&
&entry key=&cc& value=&tom&&&/entry&
&/property&
&!-- properties --&
&property name=&properties&&
&prop key=&driverclass&&com.mysql.jdbc.Driver&/prop&
&prop key=&username&&root&/prop&
&/property&
IOC(控制反转)和DI(依赖注入)的关系:(1)IOC: 控制反转,把对象创建交给spring进行配置(2)DI: 依赖注入,向类里面的属性中设置值(3)关系:依赖注入不能单独存在,需要在ioc基础之上完成操作&&&&&&&如果对你有帮助,记得点赞哦~欢迎大家关注我的博客,可以进群一起交流学习哦~&&&&&&&&&
本文已收录于以下专栏:
相关文章推荐
注入--对象属性赋值
一:基础数据的set注入
1)实体类User
public class User {
private String user...
1 简单属性值注入
package com.xy.test1;
import org.springframework.beans.factory.annotation.V
import o...
spring提供了filed的值注入和method的返回值注入。
1、Field值的注入
filed值注入需要使用org.springframework.beans.factory.config....
a.  字面值
把字面值包裹起来 --&
 b.引用其它 Bean  
场景假如有以下属性文件dev.properties, 需要注入下面的tagtag=123通过PropertyPlaceholderConfigurer
&property name=&loca...
实际应用中,某个实例的属性可能是另一个对象的一个属性,Spring支持将bean实例的属性值直接赋值给一个变量属性值的注入,是通过PropertyPathFactoryBean完成的,Property...
@Value注入不通过配置文件的注入属性的情况通过@Value将外部的值动态注入到Bean中,使用的情况有:
注入普通字符串
注入操作系统属性
注入表达式结果
注入其他Bean属性:注入beanInj...
在项目中,有些参数需要配置到属性文件xxx.properties中,这样做是为了维护方便,这样如果需要变动只需修改属性文件,不需要重新编译项目就可以了,非常方便。而为了使用起来方便,可以通过将属性值注...
spring可以为属性注入基本类型的值,也可以注入一个bean。当想注入非基本类型的值就得用到属性编辑器。它一般用在类型无法识别,如日期等。
实现步骤为以下两步:
1)继承PropertyEdit...
转载自:spring基础之属性注入总结
spring是开源的轻量级框架,核心主要两部分:
(1)aop:面向切面编程,扩展功能不是修改源代码实现
(2)ioc:控制反转,
他的最新文章
讲师:王哲涵
讲师:王渊命
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)Spring4.0MVC学习资料,注解自动扫描bean,自动注入bean(二)
Spring4.0的新特性我们在上一章已经介绍过了。包括它对jdk8的支持,Groovy Bean Definition DSL的支持,核心容器功能的改进,Web开发改进,测试框架改进等等。这张我们主要介绍spring4.0的自动扫描功能,以及对bean的过滤等特性进行学习。
好吧,废话少说,我们来看看代码吧。
Spring4.0的新特性我们在上一章已经介绍过了。包括它对jdk8的支持,Groovy Bean Definition DSL的支持,核心容器功能的改进,Web开发改进,测试框架改进等等。这张我们主要介绍spring4.0的自动扫描功能,以及对bean的过滤等特性进行学习。
好吧,废话少说,我们来看看代码吧。
package com.herman.ss.
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
import com.herman.ss.action.TestA
import com.herman.ss.filter.Filter1;
import com.herman.ss.filter.Filter2;
import com.herman.ss.filter.test.Filter3;
import com.herman.ss.pojo.H
import com.herman.ss.pojo.P
* @see spring4.0.0最新稳定版新特性,自动扫描bean,自动注入bean
* @author Herman.Xiong
* @date 日14:49:42
public class Test1 {
* @see spring4.0自动扫描bean,自动注入bean
public static void test0(){
//1.加载配置文件
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext1.xml");
//2.获取bean实例
Person person=(Person)ctx.getBean("person");
House house=(House)ctx.getBean("house");
//3.打印bean属性
System.out.println(person);
System.out.println(house);
* @see spring4.0简单业务逻辑的注解
public static void test1(){
//1.加载配置文件
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext1.xml");
//2.获取bean实例 获取bean
TestAction testAction=(TestAction)ctx.getBean("testAction");
//3.打印bean属性
System.out.println(testAction);
//4.调用bean对象的方法
testAction.testAction();
//@Service 用于标注业务层组件;
//@Repository 用于标注数据访问层组件;
//@Controller 用于标注控制层组件(如:Struts中的action)
//@Component 表示泛型组件,当组件不好归类的时候,我们可以使用这个组件进行注解。
* @see spring4.0简单注解的排除过滤器配置
public static void test2(){
//1.加载配置文件
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext1.xml");
//2.获取bean实例,只能根据bean的id获取bean
Filter1 filter1=(Filter1)ctx.getBean("filter1");
Filter2 filter2=(Filter2)ctx.getBean("filter2");
//3.打印bean属性
System.out.println(filter1);
System.out.println(filter2);
* 运行会报错:Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'filter2' is defined
* 原因是:filter2被我们排除在外了,不会自动注入
* 因此会抛异常
* @see spring4.0简单注解的包含过滤器配置
public static void test3(){
//1.加载配置文件
ApplicationContext ctx=new ClassPathXmlApplicationContext("com/herman/ss/config/applicationContext1.xml");
//2.获取bean实例
Filter3 filter3=(Filter3)ctx.getBean("filter3");
Filter2 filter2=(Filter2)ctx.getBean("filter2");
Filter1 filter1=(Filter1)ctx.getBean("filter1");
//3.打印bean属性
System.out.println(filter3);
System.out.println(filter2);
System.out.println(filter1);
* 运行会报错:Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'filter2' is defined
* 原因:filter2 被我们排除在外了
* 因此:我们回去filter2
这个bean对象的时候就会报错。
为什么不报错呢,因为我们设置了 com.herman.ss.filter包下面的use-default-filters="true"
* 因此:filter1 不会报错
public static void main(String[] args) {
* 注解需要的jar包列举:
* spring-aop-4.0.6.RELEASE.jar
* spring-beans-4.0.6.RELEASE.jar
* spring-context-4.0.6.RELEASE.jar
* spring-core-4.0.6.RELEASE.jar
* spring-expression-4.0.6.RELEASE.jar
* commons-lang-2.4.jar
//test0();
//test1();
//test2();
配置文件源码:
&?xml version="1.0" encoding="UTF-8"?&
&beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"&
&!-- 打开Spring组件自动扫面,并配置要扫描的基本包 --&
&context:component-scan base-package="com.herman.ss.pojo"&&/context:component-scan&
&context:component-scan base-package="com.herman.ss.action"&&/context:component-scan&
&context:component-scan base-package="com.herman.ss.biz"&&/context:component-scan&
&context:component-scan base-package="com.herman.ss.dao"&&/context:component-scan&
&context:component-scan base-package="com.herman.ss.filter" use-default-filters="false"&
&!-- 取消自动注入,配置只注入com.herman.ss.filter.test下面的所有类 --&
&context:include-filter type="regex" expression="com.herman.ss.filter.test.*"/&
&/context:component-scan&
&context:component-scan base-package="com.herman.ss.filter" use-default-filters="true"&
&!-- 自动注入,但是Filter2除外 --&
&context:exclude-filter type="regex" expression="com.herman.ss.filter.Filter2" /&
&/context:component-scan&
注:&context:component-scan&节点用于通知Spring容器扫描组件,base-package属性用于指定将要被扫描的组件所在的包名
这里将自动的配置扫描com.herman.ss.pojo下面的bean
&/beans&实体类源码:package com.herman.ss.
import org.springframework.beans.factory.annotation.A
import org.
* @see 实体类使用Component注解
* @author Herman.Xiong
* @date 日17:11:59
@Component("person")
public class Person {
//这里设置自动注入
@Autowired
public String getName() {
public void setName(String name) {
this.name =
public int getAge() {
public void setAge(int age) {
this.age =
public House getHouse() {
public void setHouse(House house) {
this.house =
public Person() {
public Person(String name, int age) {
this.name =
this.age =
public Person(String name, int age, House house) {
this.name =
this.age =
this.house =
public String toString() {
return "Person [age=" + age + ", house=" + house + ", name=" + name
House.java源码:package com.herman.ss.
import org.
@Component("house")
public class House {
public String getName() {
public void setName(String name) {
this.name =
public String getAddress() {
public void setAddress(String address) {
this.address =
public float getPrice() {
public void setPrice(float price) {
this.price =
public House() {
public House(String name, String address, float price) {
this.name =
this.address =
this.price =
public String toString() {
return "House [address=" + address + ", name=" + name + ", price="
+ price + "]";
package com.herman.ss.
import org.springframework.beans.factory.annotation.A
import org.springframework.stereotype.C
import com.herman.ss.biz.TestB
* @see 模拟action
* @author Herman.Xiong
* @date 日17:17:16
* @since jdk 1.6,tomcat 6.0
@Controller("testAction")
public class TestAction {
//使用自动载入
@Autowired
private TestBiz testB
//必须提供set方法
public void setTestBiz(TestBiz testBiz) {
this.testBiz = testB
public TestAction(){
System.out.println("模拟的action类");
public void testAction(){
testBiz.testBiz();
package com.herman.ss.
* @see 模拟biz层进行注解
* @author Herman.Xiong
* @date 日17:20:25
public interface TestBiz {
void testBiz();
package com.herman.ss.biz.
import org.springframework.beans.factory.annotation.A
import org.springframework.stereotype.S
import com.herman.ss.biz.TestB
import com.herman.ss.dao.TestD
@Service("testBiz")
public class TestBizImpl implements TestBiz{
@Autowired
private TestDao testD
//必须提供set方法
public void setTestDao(TestDao testDao) {
this.testDao = testD
public void testBiz() {
System.out.println("模拟biz层");
testDao.testDao();
package com.herman.ss.
* @see 模拟dao层进行注解
* @author Herman.Xiong
* @date 日17:20:25
public interface TestDao {
void testDao();
package com.herman.ss.dao.
import org.springframework.stereotype.R
import com.herman.ss.dao.TestD
@Repository("testDao")
public class TestDaoImpl implements TestDao{
public void testDao() {
System.out.println("模拟dao层");
package com.herman.ss.
import org.springframework.context.annotation.S
import org.springframework.stereotype.C
//Scope注解设置作用域
@Controller("filter1")@Scope("prototype")
public class Filter1 {
public Filter1(){
System.out.println("我是Filter1 ...");
System.out.println("Scope注解设置作用域");
package com.herman.ss.filter.
import org.springframework.stereotype.C
@Controller("filter3")
public class Filter3 {
public Filter3(){
System.out.println("我是filter3");
package com.herman.ss.
import org.springframework.stereotype.C
@Controller("filter2")
public class Filter2 {
public Filter2(){
System.out.println("我是Filter2 ...");
欢迎大家关注我的个人博客!!!!
如有不懂,疑问或者欠妥的地方,请加QQ群:
进行反馈,共同学习!
版权声明:本文内容由互联网用户自发贡献,本社区不拥有所有权,也不承担相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至: 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
用云栖社区APP,舒服~
【云栖快讯】红轴机械键盘、无线鼠标等753个大奖,先到先得,云栖社区首届博主招募大赛9月21日-11月20日限时开启,为你再添一个高端技术交流场所&&
是根据用户的业务需求和策略,经济地自动调整其弹性计算资源的管理服务,能够在业务增长时自动增加 ECS 实例,并在...
业内领先的面向企业的一站式研发提效平台,通过项目流程管理和专项自动化提效工具,能够很好地支持互联网敏捷项目的快速...
是一种简单易用的云计算资源管理和自动化运维服务。用户通过模板描述多个云计算资源的依赖关系、配置等,并自动完成所有...
为您提供简单高效、处理能力可弹性伸缩的计算服务,帮助您快速构建更稳定、安全的应用,提升运维效率,降低 IT 成本...
MaxCompute75折抢购
Loading...}

我要回帖

更多关于 springmvc注入request 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信