1.基本看了一下,spring就是利用这个框架帮助我们实例化对象的工具。首先我们需要引入jar包,pom.xml如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.zdsoft</groupId> <artifactId>mybatis-test</artifactId> <version>1.0.0</version> <dependencies> <!-- mybatis库 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.4</version> </dependency> <!-- json序列化 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.2</version> </dependency> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <!-- 日志输出 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <!-- 数据库访问,mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!-- spring核心包,共四个包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>4.1.4.RELEASE</version> </dependency> </dependencies> <build> <finalName>mybatis_test</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
最主要是最后四个核心包。
2.在根目录下创建xml文件applicationContext.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"> <!-- 实例化对象,类似于user=new zdsoft.entity.User() --> <bean id="user" class="zdsoft.entity.User"> <!-- 调用setName方法并将lisi这个值传进去 --> <property name="name" value="lisi"></property> </bean> <!-- 需要实例化UserDaoImpl和UserService类 相当于: userDaoImpl=new zdsoft.entity.UserDaoImpl(); userService=new zdsoft.entity.UserService(userDaoImpl); //注意:构造函数的名称必须是userDao,无法实例化 --> <bean id="userDaoImpl" class="zdsoft.entity.UserDaoImpl"></bean> <bean id="userService" class="zdsoft.entity.UserService"> <constructor-arg name="userDao" ref="userDaoImpl"></constructor-arg> </bean> </beans>
3.测试代码如下:
package zdsoft.entity; import static org.junit.Assert.*; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserTest { @Test public void testUser() { // 1. 加载Spring配置文件,根据创建对象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 2. 得到配置创建的对象 User user = (User) context.getBean("user"); System.out.println(user); user.add(); } @Test public void testUserService() { // 1. 加载Spring配置文件,根据创建对象 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 2. 得到配置创建的对象 UserService u = (UserService) context.getBean("userService"); u.add(); } }
上面主要就是一个基本的结构,不多说了,下面列出一下关键知识点方便今后回忆。
- 注入复杂属性,如List、Map、Properties等
- 注解开发,需要导入AOP的jar包:spring-aopjar包
- 注解创建对象@Component、@Controller、@Servlet、@Repository、@Service
- 注解注入属性@Autowired或@Resource
-
注:配置文件和注解混合使用
1)创建对象的操作一般使用配置文件方式实现;
2)注入属性的操作一般使用注解方式实现。
原文参考:https://blog.csdn.net/qq_15096707/article/details/72819930