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

相关文章
|
1月前
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
153 26
|
2月前
|
缓存 Java 数据库
SpringBoot缓存注解使用
Spring Boot 提供了一套方便的缓存注解,用于简化缓存管理。通过 `@Cacheable`、`@CachePut`、`@CacheEvict` 和 `@Caching` 等注解,开发者可以轻松地实现方法级别的缓存操作,从而提升应用的性能和响应速度。合理使用这些注解可以大大减少数据库的访问频率,优化系统性能。
213 89
|
1天前
|
JSON 前端开发 Java
Spring MVC常用的注解
@RequestMapping:用于处理请求 url 映射的注解,可用于类或方法上。用于类上,则表示类中 的所有响应请求的方法都是以该地址作为父路径。 @RequestBody:注解实现接收http请求的json数据,将json转换为java对象。 @ResponseBody:注解实现将conreoller方法返回对象转化为json对象响应给客户。 @Controller:控制器的注解,表示是表现层,不能用用别的注解代替 @RestController : 组合注解 @Conntroller + @ResponseBody @GetMapping , @PostMapping , @Put
|
3天前
|
Java 测试技术 Spring
SpringBoot+@Async注解一起用,速度提升
本文介绍了异步调用在高并发Web应用性能优化中的重要性,对比了同步与异步调用的区别。同步调用按顺序执行,每一步需等待上一步完成;而异步调用无需等待,可提升效率。通过Spring Boot示例,使用@Async注解实现异步任务,并借助Future对象处理异步回调,有效减少程序运行时间。
|
3月前
|
Java Spring
【Spring】方法注解@Bean,配置类扫描路径
@Bean方法注解,如何在同一个类下面定义多个Bean对象,配置扫描路径
199 73
|
1天前
|
Java Spring
Spring Boot的核心注解是哪个?他由哪几个注解组成的?
Spring Boot的核心注解是@SpringBootApplication , 他由几个注解组成 : ● @SpringBootConfiguration: 组合了- @Configuration注解,实现配置文件的功能; ● @EnableAutoConfiguration:打开自动配置的功能,也可以关闭某个自动配置的选项 ● @ComponentScan:Spring组件扫描
|
1月前
|
监控 Java Spring
SpringBoot:SpringBoot通过注解监测Controller接口
本文详细介绍了如何通过Spring Boot注解监测Controller接口,包括自定义注解、AOP切面的创建和使用以及具体的示例代码。通过这种方式,可以方便地在Controller方法执行前后添加日志记录、性能监控和异常处理逻辑,而无需修改方法本身的代码。这种方法不仅提高了代码的可维护性,还增强了系统的监控能力。希望本文能帮助您更好地理解和应用Spring Boot中的注解监测技术。
72 16
|
3月前
|
Java Spring 容器
【SpringFramework】Spring IoC-基于注解的实现
本文主要记录基于Spring注解实现IoC容器和DI相关知识。
71 21
|
3月前
|
存储 Java Spring
【Spring】获取Bean对象需要哪些注解
@Conntroller,@Service,@Repository,@Component,@Configuration,关于Bean对象的五个常用注解
101 12
|
3月前
|
存储 Java 应用服务中间件
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式