Spring 使用注解装配Bean

来源:互联网 发布:生食熟食知乎 编辑:程序博客网 时间:2024/05/16 10:38

Spring可以使用xml配置文件来装配bean,也可以使用注解来装配Bean

1.在上一篇文章的基础上在com.springtest包中新建Tire类,源码为:

package com.springtest; public class Tire {         privatedouble price;         privateString brand;          publicString getBrand() {                   returnbrand;         }          publicvoid setBrand(String brand) {                   this.brand= brand;         }          publicdouble getPrice() {                   returnprice;         }          publicvoid setPrice(double price) {                   this.price= price;         }}


2.新建Car类,源码为

package com.springtest; import javax.annotation.Resource; importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value; class Car {         @Autowired         privateTire tire;                 @Value("8000")         privatedouble distance;                 privatePerson owner;         publicCar(){}         publicCar (Tire tire,double distance,Person owner){                   this.tire= tire;                   this.distance= distance;                   this.owner= owner;         }//      publicTire getTire() {//               returntire;//      }        //      @Autowired//      publicvoid setTire(Tire tire) {//               this.tire= tire;//      }         publicdouble getDistance() {                   returndistance;         }         publicvoid setDistance(double distance) {                   this.distance= distance;         }         publicPerson getOwner() {                   returnowner;         }                 @Autowired         publicvoid setOwner(Person owner) {                   this.owner= owner;         }                 publicvoid display(){                   Stringmessage = "this car's tire is"+this.tire.getBrand()+";it'sdistance is"+this.distance+";it's owner is "+this.owner.getName();                   System.out.print(message);         }} 


3.在src目录下新建springAnnotation.xml配置文件,源码为:

<?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"   xsi:schemaLocation="http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans-4.2.xsd             http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-4.2.xsd             ">          <!--使用基于注解的方法自动装配 -->         <context:annotation-config/>         <!--通过属性注入的方式 -->  <bean id="tire" class="com.springtest.Tire">      <property name="price" value="1000"/>      <property name="brand" value="长城"></property>  </bean>          <!--通过构造器注入 -->         <beanid="person" class="com.springtest.Person">      <constructor-arg value="jade yoon"/>      <constructor-arg value="25"/>  </bean>    <!-- 注入list -->  <bean id="car" class = "com.springtest.Car">               <property name ="distance" value="10000000" />  </bean></beans>


4.MainApp文件的源码改为:

package com.springtest; importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp {          publicstatic void main(String[] args) {//               //TODO Auto-generated method stub//               ApplicationContextctx = new ClassPathXmlApplicationContext("bean.xml");//              //               Personperson1 = (Person)ctx.getBean("person");//               person1.sayHello();//              //               Studentstudent1 = (Student)ctx.getBean("student");//               student1.display();                                     //TODO Auto-generated method stub                   ApplicationContextctx = new ClassPathXmlApplicationContext("springAnnotation.xml");                                       CarpCar = (Car)ctx.getBean("car");                   pCar.display();                           } }


5.运行程序

0 0