继续上一篇http://blog.csdn.net/acmman/article/details/43889483的介绍
3.注解@Required
是初始化的时候就给注入,检查这个配置是否有问题。用来容错的。
4.@Resource(重要)
a)加入:j2ee/common-annotation.jar
@Resource是j2ee的标准。
b)默认按名称,名称找不到,按类型(比AutoWired更直观)
beans.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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<bean id="u" class="cn.edu.hpu.dao.Impl.UserDaoImpl">
<property name="daoId" value="1"></property>
</bean>
<bean id="userService" class="cn.edu.hpu.service.UserService">
</bean>
</beans>
UserService.java:
package cn.edu.hpu.service;
import javax.annotation.Resource;
import cn.edu.hpu.dao.UserDao;
import cn.edu.hpu.model.User;
public class UserService {
private UserDao userDao;
public void init(){
System.out.println("init");
}
public UserDao getUserDao() {
return userDao;
}
@Resource
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(User u){
this.userDao.save(u);
}
public void destroy(){
System.out.println("destroy");
}
}
测试:
package cn.edu.hpu.service;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.edu.hpu.dao.UserDao;
import cn.edu.hpu.model.User;
public class UserServiceTest {
@Test
public void testAdd() throws Exception{
ClassPathXmlApplicationContext ctx=new
ClassPathXmlApplicationContext("beans.xml");
UserService userService=(UserService)ctx.getBean("userService");
System.out.println(userService.getUserDao());
ctx.destroy();
}
}
结果:
daoid=1
c)可以指定特定名称
beans.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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<bean id="u" class="cn.edu.hpu.dao.Impl.UserDaoImpl">
<property name="daoId" value="1"></property>
</bean>
<bean id="u2" class="cn.edu.hpu.dao.Impl.UserDaoImpl">
<property name="daoId" value="2"></property>
</bean>
<bean id="userService" class="cn.edu.hpu.service.UserService">
</bean>
</beans>
UserService.java中改一下这里
@Resource(name="u2")
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
测试:
package cn.edu.hpu.service;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.edu.hpu.dao.UserDao;
import cn.edu.hpu.model.User;
public class UserServiceTest {
@Test
public void testAdd() throws Exception{
ClassPathXmlApplicationContext ctx=new
ClassPathXmlApplicationContext("beans.xml");
UserService userService=(UserService)ctx.getBean("userService");
System.out.println(userService.getUserDao());
ctx.destroy();
}
}
结果:
daoid=2
d)推荐使用
原因是比比AutoWired更直观
e)不足:如果没有源码,就无法运用annotation,只能用xml.
5.@Component(spring自带)
如果不想在beans.xml里面写bean了,想一劳永逸,让spring去指定包下去寻找,那么就用
<context:component-scan base-package=”XXXX”>其中”XXXX”就是指定的包名。
实验:
beans.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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="cn.edu.hpu"/>
<bean id="userService" class="cn.edu.hpu.service.UserService">
</bean>
</beans>
UserDaoImpl.java:
package cn.edu.hpu.dao.Impl;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.stereotype.Component;
import cn.edu.hpu.dao.UserDao;
import cn.edu.hpu.model.User;
@Component
public class UserDaoImpl implements UserDao{
public void save(User u) {
System.out.println("add success!!");
}
}
测试:
package cn.edu.hpu.service;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.edu.hpu.dao.UserDao;
import cn.edu.hpu.model.User;
public class UserServiceTest {
@Test
public void testAdd() throws Exception{
ClassPathXmlApplicationContext ctx=new
ClassPathXmlApplicationContext("beans.xml");
UserService userService=(UserService)ctx.getBean("userService");
User u=new User();
u.setUsername("u1");
u.setPassword("p1");
userService.add(u);
ctx.destroy();
}
}
结果:
add success!!
a)初始化的名字默认为类名字母小写
推荐写上名字
beans.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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="cn.edu.hpu"/>
</beans>
UserService.java:
package cn.edu.hpu.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import cn.edu.hpu.dao.UserDao;
import cn.edu.hpu.model.User;
@Component("userService")
public class UserService {
private UserDao userDao;
public void init(){
System.out.println("init");
}
public UserDao getUserDao() {
return userDao;
}
@Resource(name="u")
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(User u){
this.userDao.save(u);
}
public void destroy(){
System.out.println("destroy");
}
}
UserDaoImpl.java:
package cn.edu.hpu.dao.Impl;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.stereotype.Component;
import cn.edu.hpu.dao.UserDao;
import cn.edu.hpu.model.User;
@Component("u")
public class UserDaoImpl implements UserDao{
public void save(User u) {
System.out.println("add success!!");
}
}
测试:
package cn.edu.hpu.service;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.edu.hpu.dao.UserDao;
import cn.edu.hpu.model.User;
public class UserServiceTest {
@Test
public void testAdd() throws Exception{
ClassPathXmlApplicationContext ctx=new
ClassPathXmlApplicationContext("beans.xml");
UserService userService=(UserService)ctx.getBean("userService");
User u=new User();
u.setUsername("u1");
u.setPassword("p1");
userService.add(u);
ctx.destroy();
}
}
结果:
add success!!
b)可以指定初始化bean的名字
目前@Component、@Repository(数据仓库)、@Service、@Controller没有区别
有源码用Annotation方便,开发效率高,没源码用XML。
尊重开源精神,尊重劳动成果,转载请注明出处:http://blog.csdn.net/acmman/article/details/43889503
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/109545.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...