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
- Spring 使用注解装配Bean
- Spring IOC--Bean的装配(使用注解定义Bean)
- Spring IOC--Bean的装配(使用注解定义Bean)
- spring注解自动装配Bean
- spring-装配Bean 基于注解
- spring装配bean(基于注解)
- Spring 装配Bean 基于注解
- Spring注解方式装配Bean
- spring实战-注解装配bean
- spring实战-注解装配bean
- Spring-装配Bean 基于注解
- 使用注解+xml配置完成spring的bean的装配
- Spring Bean装配--@Bean注解注入说明
- 使用注解自动化装配Bean
- Spring使用注解装配
- Spring自动注解装配、自动检测装配Bean配合过滤组件使用
- 重温Spring之旅3——装配Bean(手工装配:使用注解方式)
- Spring常用注解,自动扫描装配Bean
- Spring 4.2入门之HelloWorld
- leetcode之Minimum Path Sum
- JAVA 中数组的几种排序方法
- 多变量对因变量贡献率分析-基于黄河中游输沙变化影响因素的nature文章
- 原码 反码 补码
- Spring 使用注解装配Bean
- ios 关闭 margin
- java之路的工作总结
- spring MVC框架简介
- hadoop快速安装部署
- Eclipse debug的快捷键
- 各种品牌进入Bios方式
- c#访问各数据库的providerName各驱动
- jcl-over-slf4j log桥接工具简介