Spring Framework Learning (6) Configuring Beans with IOC annotations

Source: Internet
Author: User

Content from: Configuring Beans with IOC annotations

Context Layer:
Context/Container Environment Applicationcontext.xml

1 IOC annotation feature

Annotations Simplify XML file configuration such as hibernate mapping file

IOC annotations simplify the configuration of IOC containers

1 Bean Object Definition Process

@Component

This annotation is used to label a class

Annotate which classes need to be managed/instantiated with IOC

The class that is labeled becomes an IOC-instantiated object

When the IOC container is parsed, all classes that are labeled by the annotation are scanned in the project and instantiated with the IOC method.

@Controller for labeling business logic objects Xxservlet xxaction Xxcontroller

@Service used to label service type objects Xxservice Xxserviceimpl

@Repository used to label persisted objects Xxdao Xxdaoimpl the use and effect of these three annotations is exactly the same as @component for the normative development of the program, as far as possible what type of object to use what annotations to label.

2 The injection process between beans

@Autowired automatically loaded. This annotation is used to label a property in the target object, and to find the matching bean in the IOC container based on the attribute name and property type of the label attribute, thus obtaining the desired bean object.

Specific examples:
Now add the context layer support in the IOC container:
Includes adding Xmlns:context, Xsi:schema, annotated scan addresses

<Beansxmlns= "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/beans http://www.springframework.org/schema/beans/ Spring-beans-3.2.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.2.xsd ">    <!--IOC annotation function ~ Context layer Import the corresponding namespace and Schame file for the context layer if the package structure is too many, directly scan com -    <Context:component-scanBase-package= "Com.etoak.action,com.etoak.dao"></Context:component-scan></Beans>

Loginaction.java:
Note that the parameters in @component la can be written or not, write the ID value of the Bean object specified, the default is the class name beginning with the letter lowercase loginaction.

@Component ("La")//<bean id= "la" class= "xx. Loginaction "/> Public classloginaction {@AutowiredPrivateUserdaoimpl ud; /*** First find a id= "UD" bean in the IOC container based on the attribute name ' UD ' of the attribute being labeled * and then find a class in the IOC container based on the attribute type of the labeled attribute ' Userdaoimpl ' * = "Userdaoimpl" of the bean to inject *@return     */     PublicString Execute () {System.out.println ("Handling client-submitted login.action requests");        Ud.login (); return"Success"; }}

Userdaoimpl.java

@Component     //  <bean id= "Userdaoimpl" class= "xx. Userdaoimpl "/>publicclass  Userdaoimpl {    publicBoolean Login () {        System.out.println ("Connect the database to determine if the login was successful");         return true ;    }}

Test class:

 Public classTest { Public Static voidMain (string[] args) {ApplicationContext ac=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); /*** 1 When instantiating a Bean object using annotations * because no specific ID value is set for it*/loginaction la= Ac.getbean (loginaction.class); /*** 2 Although there is no manual ID value set for it * But the annotation will automatically give it an ID value * class name first letter lowercase*/Userdaoimpl ud=(Userdaoimpl) Ac.getbean ("Userdaoimpl"); /*** 3 Manually set an ID value for it*/loginaction LA2= (loginaction) ac.getbean ("La"); }}

Spring Framework Learning (6) Configuring Beans with IOC annotations

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.