Java Framework Spring Boot learning Note (11): Bean Management (Annotation and configuration file mix)

Source: Internet
Author: User

Configuration files and annotations mixed use

    • Create object operations using the configuration file method
    • The operation of the injected attribute is implemented using annotations

Writing Bookdao.java and Orderdao.java files

Bookdao.java

1  Package com.example.spring; 2 3  Public class Bookdao {4      Public void Book () {5         System.out.println ("book Dao.") ); 6     }7 }

Orderdao.java

1  Package com.example.spring; 2 3  Public class Orderdao {4      Public void order () {5         System.out.println ("Order Dao.") ); 6     }7 }

Create object operations using the configuration file method

Writing a configuration file Beans.xml

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 Xmlns:context= "Http://www.springframework.org/schema/context"5 xsi:schemalocation= "Http://www.springframework.org/schema/beans6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd7 Http://www.springframework.org/schema/context8 http://www.springframework.org/schema/context/spring-context-3.0.xsd ">9 Ten     <!--turn on the annotation scan, in the package scan class, method, property above whether there are annotations - One     <Context:component-scanBase-package= "Com.example.spring"></Context:component-scan> A  -     <!--to create an object in a configuration file - -     <BeanID= "Bookservice"class= "Com.example.spring.BookService"></Bean> the     <BeanID= "Bookdao"class= "Com.example.spring.BookDao"></Bean> -     <BeanID= "Orderdao"class= "Com.example.spring.OrderDao"></Bean> -  - </Beans>

The operation of the injected attribute is implemented using annotations

Writing Bookservice.java

1  Packagecom.example.spring;2 3 ImportJavax.annotation.Resource;4 5  Public classBookservice {6     //annotation method to inject the property, get the object7    @Resource (name = "Bookdao") 8     PrivateBookdao Bookdao;9 Ten     @Resource (name = "Orderdao")  One     PrivateOrderdao Orderdao; A  -      Public voidService () { -System.out.println ("book Service.")); the         //Calling Methods - Bookdao.book (); - Orderdao.order (); -     } +}

Writing Application.java

1  Packagecom.example.spring;2 3 ImportOrg.springframework.context.support.AbstractApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6  Public classApplication {7      Public Static voidMain (string[] args) {8         //Where the bean configuration file is located d:\\ideaprojects\\spring\\src\\beans.xml9         //using the Abstractapplicationcontext containerTenAbstractapplicationcontext context =NewClasspathxmlapplicationcontext ("File:d:\\ideaprojects\\spring\\src\\beans.xml"); One         //get configuration-created objects ABookservice Bookservice = (bookservice) context.getbean ("Bookservice"); - Bookservice.service (); -     } the}

Run output

Book Service.book Dao.order Dao.

Java Framework Spring Boot learning Note (11): Bean Management (Annotation and configuration file mix)

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.