Java Framework Spring Annotation injection

Source: Internet
Author: User

First, the need to introduce a jar package: Spring-aop-4.2.4.release.jar. (within spring unpacking the package libs).

Second, if the annotation method injects the dependent object, it is necessary to refer to the new constraint.

Inside: xsd-configuration.html. Open Web page: The context schema paste copy:

1<?xml version="1.0"encoding="UTF-8"?>2<beans xmlns="Http://www.springframework.org/schema/beans"3Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"4xmlns:context="Http://www.springframework.org/schema/context"xsi:schemalocation="5http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd6http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"> <!--bean definitions here--7 8</beans>

Write interfaces and implementation classes:

1 Package Jd.com.inject; 2 3  Public Interface Indemo {4     void   Save (); 5 }

Implementation class: Need to write annotations: @Component (value = "Indemo") where value is the ID value in the configuration file, the execution method Getbean (id value) call is this value when called.

1 Package Jd.com.inject;2 3 4 import org.springframework.stereotype.Component;5 6@Component (value ="Indemo")7  Public classIndemoipl implements Indemo {8 @Override9      Public voidSave () {TenSystem. out. println ("Call the business layer. "); One     } A}

Configuration file (Applicationcontenxt.xml)

The scan component needs to be turned on:

1<?xml version="1.0"encoding="UTF-8"?>2<beans xmlns="Http://www.springframework.org/schema/beans"3Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"4xmlns:context="Http://www.springframework.org/schema/context"xsi:schemalocation="5http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd6http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"> <!--bean definitions here--7 8 9<context:component-scanBase-package="JD.com"/>Ten  One</beans>

which

<context:component-scan base-package= "JD.com"/> where base-package is the package that needs to be scanned. Note: This is written in JD.com instead of the full package. This covers all packages.

Test class:
1 Package Jd.com.inject;2 3 import org.junit.Test;4 import Org.springframework.context.ApplicationContext;5 import Org.springframework.context.support.ClassPathXmlApplicationContext;6 7  Public classTestdemo {8 @Test9      Public  voidTestdemo () {TenApplicationContext ac=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); OneIndemoiplinch= (INDEMOIPL) Ac.getbean ("Indemo"); A         inch. Save (); -     } -  the  -}

This is the implementation of annotation mode injection.


Java Framework Spring Annotation injection

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.