Spring工厂的xml配置头尾

Spring工厂的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>

=================================================================================

简化注入操作配置

<?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:p="http://www.springframework.org/schema/p"  <!-- spring3.0版本以后可以使用此功能,简化注入操作 -->
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- <bean id="person" class="ioc.Person" 
p:id="201"
p:name="wangwu"
p:age="33"
/>
</beans>

==================================================================================

在简化注入基础上再简化类关联操作

<?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:p="http://www.springframework.org/schema/p"
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="ud" class="ioc.UserDao"></bean>
<bean id="userService" class="ioc.UserService"></bean>
</beans>

注:

@Autowired  //根据类型 不常用
private UserDao userDao;
@Resource  //先根据名字再从类型查找 常用
private UserDao userDao;
============================================================================================
在以上基础上继续简化生成对象操作

<?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:p="http://www.springframework.org/schema/p"
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="ioc"></context:component-scan>   //添加此项
</beans>

注:@Service 业务层组件  @Controller 控制层组件  @Repository 数据层组件(Dao层)  @Component 泛指组件(不好归类时用此标记)

@Service
public class ProductService {

posted @ 2016-06-13 16:37  乱世_独自美  阅读(187)  评论(0)    收藏  举报