IOC实例
-
在码云上新建一个Springcase
-
修改默认的.gitignore中的内容
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# Maven #
target/
# IDEA #
.idea/
*.iml
# Eclipse #
.settings/
.classpath
.project
# MacOS #
.DS_Store
.AppleDouble
.LSOverride
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
-
从idea中引入项目
-
新建一个servicemodel模块(quickStart)
-
删除项目中自动生成的pom.xml,然后根据模板重新新建一个适合Spring的pom.xml
- spring-core
- spring-context
- spring-beans
- spring-tx
Springpom.xml模板:
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>servicemodle</artifactId>
<version>1.0-SNAPSHOT</version>
<name>servicemodle</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>5.2.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>*.properties</include>
<include>*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
-
在main下添加resources资源文件
-
引入log4j和
<build>
-
通过模板创建一个application-context.xml文件 (所有需要在Spring中进行管理的对象都需要在其中注册)
<beans>
<bean>
中定义了所有的对象- 例如:
<bean id = "myUser" class = "com.lazy.domain.Users"/>
- 属性注入:
<bean> <property name = "uname" value = "xiaoli" /></bean>
name: 指定属性 value: 为属性赋值 - ref:对象的实例 … <property name = “ptid” ref = “myPtype”/>
- 构造方法注入:
<bean> <constructor-agr name = "uname" value = "xiaozhang"/> <constructor-agr name = "upwd" value = "qaz123"/> </bean>
Springapplication-context.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>
-
在包下创建domain,创建Users类
- private String uname; private String upwd;
- 构造方法、getter and setter
-
测试类APP
Users myUser = new Users();
myUser.setUname("zzxb");
Sout打印myUser.getUname();
使用Spring 将更改为:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
Users myUser = (Users)applicationContext.getBean("myUser");
myUser.setUname("zzxb");
测试类代码:
package org.example;
import org.example.domain.Users;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application-context.xml");
Users myUser = (Users)applicationContext.getBean("myUser");
/* myUser.setUname("zzxb");*/
System.out.println(myUser.getUname());
}
}