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:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<context:annotation-config/>
<context:component-scan base-package="com.twsm"/>
</beans>
spring基础:
1.注入类型:setter注入 构造参数注入 接口注入
<bean id="ud" class="com.twsm.dao.UserDao"></bean>
<bean id="userService" class="com.twsm.service.UserService">
<!-- setter 注入 -->
<property name="userDao" ref="ud"></property>
<!-- 构造参数注入 -->
<constructor-arg index="0" ref="ud"></constructor-arg>
</bean>
调用:
ApplicationContext ctx = new ClassPathXmlApplicationContext("user-spring.xml");
UserService service = (UserService)ctx.getBean("userService");
service.addUser(u);
2. id 和 name基本可以通用,区别在于 name中可以包含一些特殊字符
3.annotation:
需要配置XML开启 annotation 功能
<context:annotation-config/>
1. @AutoWired(默认按类型) @Qualifier
2. @Resource(name="userDao")(默认按类名名称)
3. 组件: @componnent @service @controller @reponstory