Java Spring Annotation-based configuration (i)

Source: Internet
Author: User

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)

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.