Spring does not adopt a policy that is better than the configuration, and spring requires that a Java file be displayed with the specified search path. Spring will register all the appropriate Java classes as spring beans.
question: How does spring know which Java classes are being processed by the Bean class? this requires using annotation,spring to label the Bean class with some special annotation. @Component: Standard for a common spring bean class. @Controller: Labels a controller component class. @Service: Labels A business logic component class. @Repository: Labels a DAO component class.
the name of the bean instance is default to the first lowercase of the bean class, and the rest is unchanged. In a future version of Spring,@Controller,@Service,@Repository will carry more semantics. Consider using @Controller as much as possible, @Service,@Repository instead of a generic @Component. After you specify that some classes can be used as spring bean classes, it is a good idea to have spring search for the specified path, and you need to import the context Schema into the spring configuration file and specify a simple search path.
<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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp:// Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd " ><!--automatically scans all bean classes under the specified package and its sub-packages--><context:component-scanbase-package= "Org.crazyit.app.service"/>< /beans>
we can add <include-filter...> or <exclude-filter...> for <context:component-scan> child element to specify the spring Bean class, as long as the Java class at the specified path satisfies this rule, even if the Java classes do not use any of the annotation annotations, spring will treat them as if they had been bean classes. <include-filter...>: The Java class that satisfies this rule will be processed by the Bean class.
<exclude-filter...>: Specifies that the Java class that satisfies the rule will not be processed by the original Bean class.
These two child elements have two properties:Type : Specifies the filter type. Expression : Specifies the expressions required by the filter. Spring built-in supports the following four types of filters:Annotation: This filter specifies a annotation name, such as Lee. Annotationtest. assignable: Class name filter, which specifies a Java class directly. regex: A regular expression filter that specifies a regular expression in which the Java class that matches the regular expression satisfies the filtering rule, such as org\.example\.default.*. AspectJ: As Org.example. *service+.
<!--automatically scans all bean classes under the specified package and its sub-packages--><context:component-scanbase-package= "Org.crazyit.app.service" ><!-- Only classes that end in Chinese and axe are treated as beans in the spring container--><context:include-filter type= "regex" expression= ". *chinese"/>< Context:include-filter type= "regex" expression= ". *axe"/></context:component-scan>
@Resource is located under the Java.annotation package and comes from a annotation in the Java EE specification. Use this annotation to specify the collaborator Bean for the target bean. @Resource detailed usage See the classic Java EE Enterprise application practice.
@Resource has a Name property, by default, spring interprets the value as the name of the bean instance that needs to be injected.
@Controllerpublic class Demo {@Resource (name= "user") private user User, @Resource (name= "user") public void SetUser (user User) {this.user = user;} Public User GetUser () {return user;}}
@Resource can also directly modify the filed,
if the @Resource modifies the field, then the setter method of the property is not required. use @Resource to omit the Name property.
when modifying a method, omitting the Name property, the name value is the substring that the setter method removes from the preceding set string and the first letter lowercase. When you modify a field, the Name property is omitted, and the name is the same as the field. specifies the scope of the bean instance. @Scope: Annotations can also specify the scope of the bean instance.
@Controller ("demo") @Scope ("prototype") public class Demo {}
The scope of the 4 filled in, do not know that I blog, recently in retrospect spring knowledge. the @PostConstruct and @predestory are located under the Java.annotation package. used in spring to customize the life cycle behavior of beans in a spring container. @PostConstruct Decorated method is the method that precedes the initialization of the bean.
@PreDestory Retouching method is the method before the bean is destroyed.
with a deep understanding that the class uses the @PostConstruct decorated init method, spring will callback the method after the bean's dependency injection is complete.
Demo1:
@Componentpublic class chinese{//executes field injection @resource (name= "Steelaxe") private steelaxe steeaxe; public Chinese () { Super (); SYSTEM.OUT.PRINTLN ("Create Chinese class object instance ..."); @PostConstructpublic void init () {System.out.println ("initializing init method is executing ...");} @PreDestroypublic void Close () {System.out.println ("Close method before executing destruction ...");}
Create Spring container Abstractapplicationcontext CTX = Newclasspathxmlapplicationcontext ("Beans.xml");// Register close Hook Ctx.registershutdownhook ();
Print:
创建Chinese类对象实例...
创建SteelAxe类对象实例...
正在执行初始化的init方法...
正在执行销毁之前的close方法...
If you comment out Chinese dependency injection, the result is as follows:
@Componentpublic class chinese{//Execute field injection//@Resource (name= "Steelaxe")//private steelaxe steeaxe; Public Chinese () {super (); SYSTEM.OUT.PRINTLN ("Create Chinese class object instance ..."); @PostConstructpublic void init () {System.out.println ("initializing init method is executing ...");} @PreDestroypublic void Close () {System.out.println ("Close method before executing destruction ...");}
Print:
创建Chinese类对象实例...
正在执行初始化的init方法...
创建SteelAxe类对象实例...
正在执行销毁之前的close方法.
Spring Common annotation usage parsing