Spring4.0MVC学习资料,注解自动扫描bean,自动注入bean(二)

简介: <p>Spring4.0的新特性我们在上一章已经介绍过了。包括它对jdk8的支持,Groovy Bean Definition DSL的支持,核心容器功能的改进,Web开发改进,测试框架改进等等。这张我们主要介绍spring4.0的自动扫描功能,以及对bean的过滤等特性进行学习。</p> <p>好吧,废话少说,我们来看看代码吧。</p> <p></p> <pre code_snip

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   进行反馈,共同学习!

目录
相关文章
|
3月前
|
Java 测试技术 数据库
说一说 SpringBoot 整合 Junit5 常用注解
我是小假 期待与你的下一次相遇 ~
|
6月前
|
JSON 前端开发 Java
Spring MVC常用的注解
@RequestMapping:用于处理请求 url 映射的注解,可用于类或方法上。用于类上,则表示类中 的所有响应请求的方法都是以该地址作为父路径。 @RequestBody:注解实现接收http请求的json数据,将json转换为java对象。 @ResponseBody:注解实现将conreoller方法返回对象转化为json对象响应给客户。 @Controller:控制器的注解,表示是表现层,不能用用别的注解代替 @RestController : 组合注解 @Conntroller + @ResponseBody @GetMapping , @PostMapping , @Put
|
6月前
|
Java Spring
Spring Boot的核心注解是哪个?他由哪几个注解组成的?
Spring Boot的核心注解是@SpringBootApplication , 他由几个注解组成 : ● @SpringBootConfiguration: 组合了- @Configuration注解,实现配置文件的功能; ● @EnableAutoConfiguration:打开自动配置的功能,也可以关闭某个自动配置的选项 ● @ComponentScan:Spring组件扫描
|
5月前
|
人工智能 缓存 自然语言处理
保姆级Spring AI 注解式开发教程,你肯定想不到还能这么玩!
这是一份详尽的 Spring AI 注解式开发教程,涵盖从环境配置到高级功能的全流程。Spring AI 是 Spring 框架中的一个模块,支持 NLP、CV 等 AI 任务。通过注解(如自定义 `@AiPrompt`)与 AOP 切面技术,简化了 AI 服务集成,实现业务逻辑与 AI 基础设施解耦。教程包含创建项目、配置文件、流式响应处理、缓存优化及多任务并行执行等内容,助你快速构建高效、可维护的 AI 应用。
|
6月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
450 0
|
Java 开发者 Spring
解析Spring中Bean的生命周期
解析Spring中Bean的生命周期
131 2
|
XML druid Java
Spring5系列学习文章分享---第二篇(IOC的bean管理factory+Bean作用域与生命周期+自动装配+基于注解管理+外部属性管理之druid)
Spring5系列学习文章分享---第二篇(IOC的bean管理factory+Bean作用域与生命周期+自动装配+基于注解管理+外部属性管理之druid)
155 0
|
11月前
|
Java 开发者 Spring
Spring bean的生命周期详解!
本文详细解析Spring Bean的生命周期及其核心概念,并深入源码分析。Spring Bean是Spring框架的核心,由容器管理其生命周期。从实例化到销毁,共经历十个阶段,包括属性赋值、接口回调、初始化及销毁等。通过剖析`BeanFactory`、`ApplicationContext`等关键接口与类,帮助你深入了解Spring Bean的管理机制。希望本文能助你更好地掌握Spring Bean生命周期。
771 1
|
11月前
|
Java 开发者 Spring
Spring bean的生命周期详解!
本文详细介绍了Spring框架中的核心概念——Spring Bean的生命周期,包括实例化、属性赋值、接口回调、初始化、使用及销毁等10个阶段,并深入剖析了相关源码,如`BeanFactory`、`DefaultListableBeanFactory`和`BeanPostProcessor`等关键类与接口。通过理解这些核心组件,读者可以更好地掌握Spring Bean的管理和控制机制。
570 1
|
Java Spring 容器
Spring Boot 启动源码解析结合Spring Bean生命周期分析
Spring Boot 启动源码解析结合Spring Bean生命周期分析
291 11

热门文章

最新文章