Spring4.0MVC學習資料,註解自動掃描bean,自動注入bean(二)

業餘草發表於2014-07-25

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   進行反饋,共同學習!

相關文章