Note reference: 1.service.xml Configuring annotation Mode
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context /SPRING-CONTEXT-2.5.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-2.5.xsd "><!--Configure the processor for annotations - <Context:annotation-config/> <Context:component-scanBase-package="*.*"/> </Beans>
2.userservice.java
Name the class by using @service
The same is true with the @controller ("UserService") effect.
If you use @controller, then you can only use a hump like userservice.
PackageCom.sun.service;Importjava.util.ArrayList;ImportJavax.annotation.Resource;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Service; @Service ("UserService") Public classUserService {PrivateString name; PrivateArrayList arr; PublicArrayList Getarr () {returnarr; } Public voidSetarr (ArrayList arr) { This. arr =arr; } PublicUserService () {System.out.println ("This is Chushihua"); } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public voidinit () {System.out.println ("Init------------"); } Public voidCleanup () {System.out.println ("Cleanup------------"); } }
3.helloworld.java
PackageCom.sun.service;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.AbstractApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classHelloWorld { Public Static voidMain (string[] args) {//TODO auto-generated Method StubAbstractapplicationcontext app =NewClasspathxmlapplicationcontext ("Service.xml"); UserService US= (UserService) app.getbean ("UserService"); Us.setname ("Hello World"); System.out.println (Us.getname ()); //us.setname ("Sunzhiyan"); /*System.out.println (Us.getname ()); for (int i = 0;i < Us.getarr (). Size (); i++) {System.out.println (Us.getarr (). get (i)); } app.registershutdownhook ();*/ }}
This allows for output.
Java Spring Annotation-based configuration (i)