Spring4.0.6 latest stable version new feature learning, annotation automatic scan bean, automatic injection bean (2)

Source: Internet
Author: User

Spring4.0.6 latest stable version new feature learning, annotation automatic scan bean, automatic injection bean (2)

The new features of Spring4.0 have been introduced in the previous chapter. Including jdk8 support, Groovy Bean Definition DSL support, core container function improvement, Web development improvement, testing framework improvement, and so on. This section describes the automatic scanning function of spring and bean filtering and other features.

Let's look at the code.

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 new stable version of spring4.0.0 Automatically scans beans and injects beans * @ author Herman. xiong * @ date July 18, 2014 14:49:42 */public class Test1 {/*** @ see spring4.0 automatically scans beans and injects beans */public static void test0 () {// 1. load the configuration file ApplicationContext ctx = new ClassPathXmlApplicationContext ("com/herman/ss/config/applicationContext1.xml"); // 2. obtain bean instance Person person = (Person) ctx. getBean ("person"); House house = (House) ctx. getBean ("house"); // 3. print the bean attribute System. out. printl N (person); System. out. println (house);}/*** @ see spring4.0 simple business logic annotation */public static void test1 () {// 1. load the configuration file ApplicationContext ctx = new ClassPathXmlApplicationContext ("com/herman/ss/config/applicationContext1.xml"); // 2. get bean instance get beanTestAction testAction = (TestAction) ctx. getBean ("testAction"); // 3. print the bean attribute System. out. println (testAction); // 4. call the method testAction of the bean object. testAction (); // @ Service is used to mark Service layer components; // @ Rep Ository is used to mark data access layer components; // @ Controller is used to mark control layer components (for example, actions in Struts) // @ Component indicates generic components. When components are difficult to classify, we can use this component for annotation .} /*** @ See spring4.0 simple annotation exclusion filter configuration */public static void test2 () {// 1. load the configuration file ApplicationContext ctx = new ClassPathXmlApplicationContext ("com/herman/ss/config/applicationContext1.xml"); // 2. obtain the bean instance. Only beanFilter1 filter1 = (Filter1) ctx can be obtained based on the bean id. getBean ("filter1"); Filter2 filter2 = (Filter2) ctx. getBean ("filter2"); // 3. print the bean attribute System. out. println (filter1); System. out. println (filter2);/*** an error is reported during running: Exception in thre Ad "main" org. springframework. beans. factory. noSuchBeanDefinitionException: No bean named 'filter2' is defined * cause: filter2 is excluded, * will not be automatically injected, so an exception will be thrown */}/*** @ see spring4.0 simple annotation containing the filter configuration */public static void test3 () {// 1. load the configuration file ApplicationContext ctx = new ClassPathXmlApplicationContext ("com/herman/ss/config/applicationContext1.xml"); // 2. obtain the bean instance Filter3 filter3 = (Filter3) ctx. getBean ("filter3"); Filter2 fil Ter2 = (Filter2) ctx. getBean ("filter2"); Filter1 filter1 = (Filter1) ctx. getBean ("filter1"); // 3. print the bean attribute System. out. println (filter3); System. out. println (filter2); System. out. println (filter1);/*** an error is reported during running: Exception in thread "main" org. springframework. beans. factory. noSuchBeanDefinitionException: No bean named 'filter2' is defined * cause: filter2 is excluded * therefore: An error will be reported when we go back to the filter2 bean object. * Why does filter1 not report an error, because we set com. herman. ss. use-default-filters = "true" automatic import under the filter package * therefore: filter1 will not report an error */} public static void main (String [] args) {/*** annotation required jar package list: * 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 ();}}
Source code of the configuration file:

 
              
        
      
      
      
      
      
       
       
       
      
       
        
  
  
 
Entity source code:
Package com. herman. ss. pojo; import org. springframework. beans. factory. annotation. autowired; import org. springframework. stereotype. component;/*** @ see object class using Component annotation * @ author Herman. xiong * @ date July 24, 2014 17:11:59 */@ Component ("person") public class Person {private String name; private int age; // set Automatic injection here @ Autowiredprivate 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 ;}@ Overridepublic String toString () {return "Person [age =" + age + ", house =" + house + ", name = "+ name +"] ";}}
House. java source code:
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 simulate action * @ author Herman. xiong * @ date July 24, 2014 17:17:16 * @ since jdk 1.6, tomcat 6.0 */@ Controller ("testAction") public class TestAction {// use automatic loading @ Autowiredprivate TestBiz testBiz; // The set Method public void setTestBiz (TestBiz testBiz) {this. testBiz = testBiz;} public TestAction () {System. out. println ("simulated action class");} public void testAction () {testBiz. testBiz ();}}
Package com. herman. ss. biz;/*** @ see simulate the biz layer for annotation * @ author Herman. xiong * @ date July 24, 2014 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 {@ Autowiredprivate TestDao testDao; // The set Method public void setTestDao (TestDao testDao) {this. testDao = testDao;} public void testBiz () {System. out. println ("simulate biz layer"); testDao. testDao ();}}
Package com. herman. ss. dao;/*** @ see simulate dao layer annotation * @ author Herman. xiong * @ date July 24, 2014 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 ("simulate dao layer ");}}
Package com. herman. ss. filter; import org. springframework. context. annotation. scope; import org. springframework. stereotype. controller; // Scope annotation setting Scope @ Controller ("filter1") @ Scope ("prototype") public class Filter1 {public Filter1 () {System. out. println ("I'm Filter1... "); System. out. println ("Scope annotation setting Scope ");}}
Package com. herman. ss. filter. test; import org. springframework. stereotype. controller; @ Controller ("filter3") public class Filter3 {public Filter3 () {System. out. println ("I am filter3 ");}}
Package com. herman. ss. filter; import org. springframework. stereotype. controller; @ Controller ("filter2") public class Filter2 {public Filter2 () {System. out. println ("I'm Filter2... ");}}

Welcome to follow my blog !!!!

If you have any questions or problems, add the QQ group: 135430763 for feedback and learn together!












Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.