Spring 使用注解装配Bean

原文链接: https://yq.aliyun.com/articles/17244

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配置文件,源码为:


 

 
         
         
         
  
      
      
  
 
         
         
      
      
  
  
  
  
               
  


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.运行程序

你可能感兴趣的:(Spring 使用注解装配Bean)