<?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-3.2.xsd">
</beans>
- 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-3.2.xsd">
<!--
通过bean元素声明需要Spring创建的实例。该实例的类型通过class属性指定,
并通过id属性为该实例指定一个名称,以便在程序中使用-->
<!-- User。定义User bean,该bean的id是user ,class指定该bean实例的实现类-->
<!-- 也可以通过name获取,name可以设置多个值,用逗号隔开-->
<bean id="user" class="cn.gwj.entity.User" name="t1,t2">
<!-- property元素用来为实例的属性赋值,此处实际是调用setuserName()方法实现赋值操作 -->
<!-- 此处将字符串"张三"赋值给userName属性 -->
<property name="userName" value="张三"/>
<property name="gender" value="0"/>
<!-- 通过ref属性注入已经定义好的bean-->
<property name="role" ref="role0"/>
</bean>
<bean id="role0" class="cn.gwj.entity.Role">
<property name="roleCode" value="管理员"/>
<property name="roleName" value="系统管理员"/>
</bean>
</beans>
- 测试
@Test
public void TestSpring(){
//通过ClassPathXmlApplicationContext实例化Spring的上下文
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
// String[] sr={"spring-dao.xml","spring-config.xml"};引入多个spring配置
// ApplicationContext applicationContext=new ClassPathXmlApplicationContext(sr);
//通过ApplicationContext的getBean()方法,根据id来获取bean的实例
User user=(User)applicationContext.getBean("user");//user也可换成t1或者t2
user.setUserCode("asda");
logger.info(user.getUserName());
logger.info(user.getUserCode());
logger.info(user.getRole().getRoleCode());
}
--
07-11 15:14:01[INFO]cn.gwj.dao.UserDaoTest
-张三
07-11 15:14:01[INFO]cn.gwj.dao.UserDaoTest
-asda
07-11 16:07:45[INFO]cn.gwj.dao.UserDaoTest
-管理员
- 创建maven依赖(省略jar包的导入)
<properties>
<!-- spring版本号 -->
<spring.version>4.2.5.RELEASE</spring.version>
</properties>
<!-- 添加spring核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 声明事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!--aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--aspect依赖-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.3</version>
</dependency>