Spring框架context的注解管理方法之二 使用注解注入基本类型和对象属性 注解annotation和配置文件混合使用(半注解)

时间:2023-12-27 13:34:19

首先还是xml的配置文件

<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.swift"></context:component-scan>
</beans>

接着是假定dao的类

package com.swift;

import org.springframework.stereotype.Component;

@Component(value="dao")
public class Dao {
public String fun() {
return "This is Dao's fun()........"; }
}

生成一个对象很方便,甚至@Component(value="dao")中的value=都可以不写,变成

@Component("dao")

然后是假定service的类

package com.swift;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component(value="service")
public class Service { @Autowired
private Dao dao;
public String fun() {
return "This is Service's fun()......."+"\r\n"+this.dao.fun();
}
//注意使用注解方法,不需要自己生成setter方法了
public void setDao(Dao dao) {
this.dao = dao;
} }

与配置文件中使用<bean id="service" class="com.swift.Service"><property name="dao" ref="dao"></property></bean>

不同,注解生成两个对象后,再注解属性

@Autowired

就搞定了,自动装配,自动连线

最后使用Servlet来测试一下

package com.swift;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@WebServlet("/test")
public class ServletTest extends HttpServlet {
private static final long serialVersionUID = 1L;
public ServletTest() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
ApplicationContext context=new ClassPathXmlApplicationContext("zhujie.xml");
Service service=(Service) context.getBean("service");
String test=service.fun();
response.getWriter().append(test);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }

浏览器结果如下

Spring框架context的注解管理方法之二 使用注解注入基本类型和对象属性 注解annotation和配置文件混合使用(半注解)

自动装载的这种方法   @Autowired    原理是通过类名找到定义的对象,这种注解使用不多,因为多个对象存在的话,注入的是哪个?

所以,使用

另一个注解,可以明确到底注入哪个对象

@Resource(name="dao")
private Dao dao;

这种方法使用较多

 注入基本类型和对象属性

半注解方式

package cn.itcast.domain;

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; //<bean name="user" class="cn.itcast.domain.User" />
@Component("user")
/* //注册service层对象
@Service
@Repository //注册Dao层对象
@Controller //注册Web层对象*/
//<bean scope="singleton|prototype" >
@Scope("prototype")
public class User {
@Value("tom") //为name赋值为tom
private String name;
private Integer age;
@Resource(name="car2")
/*
* @Autowired 自动注入 有就注入 默认名car开始
* 注意:如果匹配到多个会抛出异常*/
// @Autowired
/*
* 当自动注入匹配到多个对象时,可以使用@Qualifier 指定具体注入哪一个(不常用)
*/
@Autowired
@Qualifier("car2")
private Car car; public User() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} //将赋值注解放到set方法上,可执行方法中判断逻辑
@Value("18")//为age赋值
public void setAge(Integer age) {
System.out.println("public void setAge(Integer age)!");
this.age = age;
} public Car getCar() {
return car;
} public void setCar(Car car) {
this.car = car;
}
//<bean init-method="init" >
@PostConstruct
public void init() {
System.out.println("构造之后初始化方法!");
}
//<bean destory-method="destory" >
@PreDestroy
public void destory() {
System.out.println("销毁之前销毁方法!");
} @Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
}
}

引用类型Car

package cn.itcast.domain;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class Car { @Value("哈佛H6")
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "Car [name=" + name + "]";
} }

XML配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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 "> <!-- 开启ioc注解 -->
<context:component-scan base-package="cn.itcast"></context:component-scan> <!---->
<bean name="car1" class="cn.itcast.domain.Car" >
<property name="name" value="五菱宏光"></property>
</bean>
<bean name="car2" class="cn.itcast.domain.Car" >
<property name="name" value="长安CS95" ></property>
</bean> </beans>

测试类

package cn.itcast.a_ioc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.domain.User; public class Demo {
@Test
public void fun1(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User u1 = (User) ac.getBean("user");
User u2 = (User) ac.getBean("user"); System.out.println(u1==u2);
} @Test
public void fun2(){ ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); User u1 = (User) ac.getBean("user"); System.out.println(u1);
} }