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