码迷,mamicode.com
首页 > 编程语言 > 详细

Spring(四)注解配置Ioc

时间:2016-04-03 23:30:26      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

原文链接:http://www.orlion.ga/216/

一、@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.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">

   <context:annotation-config/>
    <bean id="userDao" class="ml.orlion.dao.impl.UserDAOImpl">
    </bean>
    
    <bean id="userService" class="ml.orlion.service.UserService">
    </bean>
</beans>

当我们在配置文件中配置了<context:annotation-config/>时,spring会自动帮我们初始化四个bean(AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor, PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor)来处理我们在代码中的注解

 

然后UserService.java:

代码中的注解@Autowired默认是byType,所以beans.xml中不能出现两个类型相同的bean,因为如果类型一致spring就不知道应该注入哪一个了。

package ml.orlion.service;

import org.springframework.beans.factory.annotation.Autowired;

import ml.orlion.dao.UserDAO;
import ml.orlion.dao.impl.UserDAOImpl;
import ml.orlion.model.User;

public class UserService {

    private UserDAO userDAO = new UserDAOImpl();

    public UserDAO getUserDao() {
        
        return userDAO;
    }
    @Autowired
    public void setUserDao(UserDAO userDAO) {
        this.userDAO = userDAO;
    }
    
    public void saveUser(User user){
        this.userDAO.saveUser(user);
    }
}

测试一下

BeanFactory appContext = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) appContext.getBean("userService");
userService.saveUser(new User());

输出saving user,即成功调用了UserDAOImpl中的save方法

 

上边的代码有一个限制就是beans.xml中一个类只能配置一个bean,比如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.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">

   <context:annotation-config/>
    <bean id="userDao" class="ml.orlion.dao.impl.UserDAOImpl">
    </bean>
    
    <bean id="userDao1" class="ml.orlion.dao.impl.UserDAOImpl">
    </bean>
    
    <bean id="userService" class="ml.orlion.service.UserService">
    </bean>
</beans>

这时候可以把UserService类改成如下(在setUserService方法中加注解)

package ml.orlion.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import ml.orlion.dao.UserDAO;
import ml.orlion.dao.impl.UserDAOImpl;
import ml.orlion.model.User;

public class UserService {

    private UserDAO userDAO = new UserDAOImpl();

    public UserDAO getUserDao() {
        
        return userDAO;
    }
    @Autowired
    public void setUserDao(@Qualifier("userDao") UserDAO userDAO) {
        this.userDAO = userDAO;
    }
    
    public void saveUser(User user){
        this.userDAO.saveUser(user);
    }
}

这时候再运行就不会报错了。

二、@Reosource

从bean容器中注入bean,默认是按名字查找,找不到再按类型,可以指定名字

 

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.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">

   <context:annotation-config/>
   
	<bean id="userDao" class="ml.orlion.dao.impl.UserDAOImpl">
	</bean>
	
	<bean id="userDao1" class="ml.orlion.dao.impl.UserDAOImpl">
	</bean>
	
	<bean id="userService" class="ml.orlion.service.UserService">
	</bean>
</beans>

UserService.java:

package ml.orlion.service;

import javax.annotation.Resource;

import ml.orlion.dao.UserDAO;
import ml.orlion.dao.impl.UserDAOImpl;
import ml.orlion.model.User;

public class UserService {

	private UserDAO userDAO = new UserDAOImpl();

	public UserDAO getUserDao() {
		
		return userDAO;
	}
	@Resource(name="userDao")
	public void setUserDao(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	public void saveUser(User user){
		this.userDAO.saveUser(user);
	}
}

测试可以运行会输出saving user

 

三、@Component

将一个类声明为spring的bean

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.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">

   <context:annotation-config/>
   <context:component-scan base-package="ml.orlion"></context:component-scan>
   
</beans>

其中<context:component-scan base-package="ml.orlion"></context:component-scan>表示从包名为ml.orlion下的所有类中找Component.

UserDAOImpl.java

package ml.orlion.dao.impl;

import org.springframework.stereotype.Component;

import ml.orlion.dao.UserDAO;
import ml.orlion.model.User;
@Component("userDao") //默认名字是userDaoImpl
public class UserDAOImpl implements UserDAO{

	public void saveUser(User user){
		System.out.println("save usering");
	}
}

UserService.java

package ml.orlion.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import ml.orlion.dao.UserDAO;
import ml.orlion.dao.impl.UserDAOImpl;
import ml.orlion.model.User;
@Component("userService")
public class UserService {

	private UserDAO userDAO = new UserDAOImpl();

	public UserDAO getUserDao() {
		
		return userDAO;
	}
	@Resource(name="userDao")
	public void setUserDao(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	public void saveUser(User user){
		this.userDAO.saveUser(user);
	}
}

测试一下:

BeanFactory appContext = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserService) appContext.getBean("userService");
userService.saveUser(new User());

运行会输出saving user

 

四、@Scope、@PostConstruct、@PreDestroy

可以在类定义之前添加注释@Scope("prototype")将bean声明为每次调用时都重新new一个。

@PostConstructbean声明在初始化时运行的方法

@PreDestroy声明在销毁时运行的方法

Spring(四)注解配置Ioc

标签:

原文地址:http://www.cnblogs.com/orlion/p/5350863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!