1.搭建环境
1.gradle文件
plugins {
id 'java'
}
group 'com.sth'
version '1.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
maven {
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
}
dependencies {
compile(
"org.springframework:spring-core:5.2.5.RELEASE",//最新的版本core DI IOC
"org.springframework:spring-aop:5.2.5.RELEASE", //
"org.springframework:spring-beans:5.2.5.RELEASE",
"org.springframework:spring-context:5.2.5.RELEASE",
"org.springframework:spring-context-support:5.2.5.RELEASE",
"org.springframework:spring-web:5.2.5.RELEASE",
"org.springframework:spring-orm:5.2.5.RELEASE", // 对象关系库
"org.springframework:spring-aspects:5.2.5.RELEASE",// 面向切面编程
"org.springframework:spring-webmvc:5.2.5.RELEASE",
"org.springframework:spring-jdbc:5.2.5.RELEASE",
"org.springframework:spring-instrument:5.2.5.RELEASE",// 字节码 增强
"org.springframework:spring-tx:5.2.5.RELEASE" // 自定义标枪
)
}
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
[compileJava, javadoc, compileTestJava]*.options*.encoding = "UTF-8"
2.创建一个pojo
package com.sth.spring.bean;
/**
* POJO : Plain Old Java Object
*/
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3.配置文件
<?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">
<bean id ="student" class="com.sth.spring.bean.Student">
<property name="name" value="sitianhong"/>
<property name="age" value="20"/>
</bean>
</beans>
4.客户端
import com.sth.spring.bean.Student;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
* IOC(Inverse of Controller,控制反转)
* DI (Dependency Injection,依赖注入)
*/
public class SpringClient {
public static void main(String[] args) {
Resource resource = new ClassPathResource("applicationContext.xml");
DefaultListableBeanFactory defaultListableBeanFactory = new DefaultListableBeanFactory();
BeanDefinitionReader beanDefinitionReader =
new XmlBeanDefinitionReader(defaultListableBeanFactory);
int a = beanDefinitionReader.loadBeanDefinitions(resource);
Student s1 =(Student)defaultListableBeanFactory.getBean("student");
}
}
… 未完待续