spring注解注入详解

1、Spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans   xmlns="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-3.0.xsd
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context-4.0.xsd">
         <!-- 
         	init-method="getstart" 该类初始化执行的方法
         	destroy-method="getdestroy"  该类被销毁时执行的方法
         	scope="singleton" 默认为单列模式 可以  不写
         	scope="prototype" 多列模式
         	<property name="该类的属性名" value="给该类属性名注入的值"></property>
         	<import resource="user.xml"/> 若有多个配置文件可以用 inport 
          -->
     <!--  <bean id="car" class="com.sumeng.web.Car" init-method="getstart" destroy-method="getdestroy" scope="">
     	<property name="carName" value="奥迪"></property>
     </bean>  
     <import resource="user.xml"/>-->
     <!--  通过 注解 注入  类扫描器 -->
     <context:component-scan base-package="com.sumeng.web"></context:component-scan>
</beans>

2、创建UserService和Car两个类

/** 

* @author 作者 Your-Name: 

* @version 创建时间:2019年5月30日 上午9:42:34 

* 类说明 

*/ 
package com.sumeng.web;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @author Administrator
 *@Component(value="car")   通用 其中car 想当于bean中的id
 *@Controller web层用
 *@Service service层用
 *@Scope 不写或者为 @Scope为单列模式 
 *@Scope("prototype") 为多列模式  prototype(多列)
 */
@Component(value="car")
@Scope("prototype")
public class Car {
	@Value("宝马") 	//给carName属性注入值
	private String carName;
	
	/**
	 * @Autowired 对象注入   ----实际开发常用 
	 * @Autowired  	@Qualifier("user") 两个一起使用  也是对象注入  其中 user相当于bean中的id
	 * @Resource(name="user")  jdk自带注解 和前两个同样的性质 
	 * 生命周期
	 * 		@PostConstruct 相当于  bean中的    init-method
	 * 		@PreDestroy  相当于bean中的   destroy-method
	 */
	
	@Resource(name="user")
	private UserService userservice;
	
	public void getCar(){
		System.out.println("我是汽车"+this.carName);
		userservice.getsave();
	}
	
	@PostConstruct
	public void getstart(){
		System.out.println("我被创建了-car!");
	}
	@PreDestroy
	public void getdestroy(){
		System.out.println("我背销毁了-car!!");
	}

	public String getCarName() {
		return carName;
	}

	public void setCarName(String carName) {
		this.carName = carName;
	}

	@Override
	public String toString() {
		return "Car [carName=" + carName + "]";
	}
	public UserService getUser() {
		return userservice;
	}
	public void setUser(UserService user) {
		this.userservice = user;
	}

}
/** 

* @author 作者 Your-Name: 

* @version 创建时间:2019年5月30日 上午10:19:45 

* 类说明 

*/ 
package com.sumeng.web;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

/**
 * @author Administrator
 *
 */
@Component(value="user") //相当于 bean 【id  class】
public class UserService {
	
	@PostConstruct		//相当于bean中的init-method
	public void init(){
		System.out.println("user对象  创建之前执行的方法");
	}
	@PreDestroy			//箱单于bean中的destroy-method
	public void destroy(){
		System.out.println("user对象 销毁后执行的方法");
	}
	public void getsave(){
		System.out.println("我是user");
	}

}

3.创建测试类

/** 

* @author 作者 Your-Name: 

* @version 创建时间:2019年5月30日 上午9:44:57 

* 类说明 

*/ 
package com.sumeng.action;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sumeng.web.Car;
import com.sumeng.web.UserService;

/**
 * @author Administrator
 *
 */
public class Test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("config/Spring.xml");
		Car car = (Car)context.getBean("car");
		Car car2 = (Car)context.getBean("car");
		System.out.println(car == car2);
		car.getCar();
	}
	

}

4、效果

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值