Spring 使用注解装配Bean

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值