Spring4.0MVC学习资料,注解自己主动扫描bean,自己主动注入bean(二)
Spring4.0的新特性我们在上一章已经介绍过了。
包含它对jdk8的支持,Groovy Bean Definition DSL的支持。核心容器功能的改进,Web开发改进。測试框架改进等等。这张我们主要介绍spring4.0的自己主动扫描功能,以及对bean的过滤等特性进行学习。
好吧。废话少说,我们来看看代码吧。
package com.herman.ss.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.herman.ss.action.TestAction;
import com.herman.ss.filter.Filter1;
import com.herman.ss.filter.Filter2;
import com.herman.ss.filter.test.Filter3;
import com.herman.ss.pojo.House;
import com.herman.ss.pojo.Person;
/**
* @see spring4.0.0最新稳定版新特性,自己主动扫描bean,自己主动注入bean
* @author Herman.Xiong
* @date 2014年7月18日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对象的时候就会报错。 * filter1 为什么不报错呢。由于我们设置了 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();
test3();
}
}
配置文件源代码:
<? 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.pojo; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @see 实体类使用Component注解
* @author Herman.Xiong
* @date 2014年7月24日17:11:59
*/
@Component("person")
public class Person {
private String name;
private int age;
//这里设置自己主动注入
@Autowired
private House house;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public House getHouse() {
return house;
}
public void setHouse(House house) {
this.house = house;
}
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person(String name, int age, House house) {
super();
this.name = name;
this.age = age;
this.house = house;
}
@Override
public String toString() {
return "Person [age=" + age + ", house=" + house + ", name=" + name
+ "]";
} }
House.java源代码:
package com.herman.ss.pojo;
import org.springframework.stereotype.Component;
@Component("house")
public class House {
private String name;
private String address;
private float price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public House() {
super();
}
public House(String name, String address, float price) {
super();
this.name = name;
this.address = address;
this.price = price;
}
public String toString() {
return "House [address=" + address + ", name=" + name + ", price="
+ price + "]";
}
}
package com.herman.ss.action; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import com.herman.ss.biz.TestBiz; /**
* @see 模拟action
* @author Herman.Xiong
* @date 2014年7月24日17:17:16
* @since jdk 1.6,tomcat 6.0
*/
@Controller("testAction")
public class TestAction { //使用自己主动加载
@Autowired
private TestBiz testBiz;
//必须提供set方法
public void setTestBiz(TestBiz testBiz) {
this.testBiz = testBiz;
} public TestAction(){
System.out.println("模拟的action类");
} public void testAction(){
testBiz.testBiz();
}
}
package com.herman.ss.biz; /**
* @see 模拟biz层进行注解
* @author Herman.Xiong
* @date 2014年7月24日17:20:25
*/
public interface TestBiz {
void testBiz();
}
package com.herman.ss.biz.impl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.herman.ss.biz.TestBiz;
import com.herman.ss.dao.TestDao;
@Service("testBiz")
public class TestBizImpl implements TestBiz{
@Autowired
private TestDao testDao; //必须提供set方法
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
} public void testBiz() {
System.out.println("模拟biz层");
testDao.testDao();
} }
package com.herman.ss.dao; /**
* @see 模拟dao层进行注解
* @author Herman.Xiong
* @date 2014年7月24日17:20:25
*/
public interface TestDao {
void testDao();
}
package com.herman.ss.dao.impl; import org.springframework.stereotype.Repository; import com.herman.ss.dao.TestDao;
@Repository("testDao")
public class TestDaoImpl implements TestDao{ public void testDao() {
System.out.println("模拟dao层");
} }
package com.herman.ss.filter; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
//Scope注解设置作用域
@Controller("filter1")@Scope("prototype")
public class Filter1 {
public Filter1(){
System.out.println("我是Filter1 ...");
System.out.println("Scope注解设置作用域");
}
}
package com.herman.ss.filter.test;
import org.springframework.stereotype.Controller;
@Controller("filter3")
public class Filter3 {
public Filter3(){
System.out.println("我是filter3");
}
}
package com.herman.ss.filter;
import org.springframework.stereotype.Controller;
@Controller("filter2")
public class Filter2 {
public Filter2(){
System.out.println("我是Filter2 ...");
}
}
欢迎大家关注我的个人博客!!!!
如有不懂,疑问或者欠妥的地方,请加QQ群:135430763 进行反馈。共同学习!
Spring4.0MVC学习资料,注解自己主动扫描bean,自己主动注入bean(二)的更多相关文章
- Spring4.0MVC学习资料,ApplicationContext中的方法具体解释(三)
做为java开源的一部分,spring框架一直排在老大的位置.Spring4.0 是 Spring 推出的一个重大版本号升级,进一步加强了 Spring 作为 Java 领域第一开源平台的地位.Spr ...
- Spring中注解注入bean和配置文件注入bean
注解的方式确实比手动写xml文件注入要方便快捷很多,省去了很多不必要的时间去写xml文件 按以往要注入bean的时候,需要去配置一个xml,当然也可以直接扫描包体,用xml注入bean有以下方法: & ...
- Spring - 基于注解的组件扫描
关于Spring的书籍都会花很大篇幅来讲解Spring如何注入各种bean的问题,非常令人头疼,自己在工作中还从来没有用到过. 所以就要跳过那些篇章,直接学习基于注解的组件扫描. 发现spring2是 ...
- Mybatis结合Spring注解自己主动扫描源代码分析
作为一个想做架构师的程序猿,必须是一个优秀的程序猿.在引入某一个框架的时候,必需要研究源代码,将新的开源框架的风险变为可控性. 1.Spring结合Mybatis最经常使用的配置. <!--理论 ...
- Spring @Autowired 注解 学习资料
Spring @Autowired 注解 学习资料 网址 Spring @Autowired 注解 https://wiki.jikexueyuan.com/project/spring/annota ...
- 深入学习JAVA注解-Annotation(学习过程)
JAVA注解-Annotation学习 本文目的:项目开发过程中遇到自定义注解,想要弄清楚其原理,但是自己的基础知识不足以支撑自己去探索此问题,所以先记录问题,然后补充基础知识,然后解决其问题.记录此 ...
- Android 学习资料收集
收集整理这份资料灵感来自于 trip_to_iOS, 征得同意引用了该资料的开头描述 收集整理这份资料主要帮助初学者学习 Android 开发, 希望能快速帮助到他们快速入门, 找到适合自己学习资料, ...
- 【转】iOS超全开源框架、项目和学习资料汇总
iOS超全开源框架.项目和学习资料汇总(1)UI篇iOS超全开源框架.项目和学习资料汇总(2)动画篇iOS超全开源框架.项目和学习资料汇总(3)网络和Model篇iOS超全开源框架.项目和学习资料汇总 ...
- Spring学习资料以及配置环境
一.Spring4 1.介绍 新特性 SpringIDE 插件 IOC DI 在 Spring 中配置 Bean 自动装配 Bean 之间的关系(依赖.继承) Bean 的作用域 使用外部属性文件 S ...
随机推荐
- 《BUG创造队》作业8:软件测试与Alpha冲刺(第四天)
项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 实验十二 团队作业8:软件测试与ALPHA冲刺 团队名称 BUG创造队 作业学习目标 (1)掌握软件测试基础技术.(2)学习 ...
- POJ - 3126 - Prime Path(BFS)
Prime Path POJ - 3126 题意: 给出两个四位素数 a , b.然后从a开始,每次可以改变四位中的一位数字,变成 c,c 可以接着变,直到变成b为止.要求 c 必须是素数.求变换次数 ...
- N皇后递归
问题: n皇后问题:输入整数n, 要求n个国际象棋的皇后,摆在 n*n的棋盘上,互相不能攻击,输出全部方案. #include <iostream> using namespace std ...
- mysql 删除恢复
一.模拟误删除数据表的恢复 1 二进制日志功能启用 vim /etc/my.cnf [mysqld] log-bin 2 完全备份 mysqldump -A -F --master-data=2 - ...
- jquery 打星评分插件
<link rel="stylesheet" href="/static/vendor/raty/jquery.raty.css"> <scr ...
- Python面向对象之文件操作
文件的概念 文件的概念和作用 计算机的文件,就是存储在某种长期存储设备上的一段数据:长期存储设备包括:U盘,硬盘,移动硬盘,光盘,等: 文件的作用:将数据长期保存,在需要的时候使用: 文件的存储方式 ...
- 洛谷 1067 NOIP2009 普及组 多项式输出
[题解] 一道简单的模拟题.需要判一些特殊情况:第一项的正号不用输出,x的一次项不用输出指数,系数为0的项不用输出等等,稍微细心一下就好. #include<cstdio> #includ ...
- 算法导论 第十二章 二叉搜索树(python)
上图: 这是二叉搜索树(也有说是查找树的)基本结构:如果y是x的左子树中的一个结点,那么y.key <= x.key(如a图中的6根结点大于它左子树的每一个结点 6 >= {2,5,5}) ...
- python基础——5(元组、字典、集合)
上节复习: # 数字类型 # int:py3|py2 long:py2 # float: 3.14 # complex(5, 4) => 5 + 4j num = 12345678901234 ...
- xtu read problem training 2 B - In 7-bit
In 7-bit Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 3 ...