Spring框架学习
spring:一站式 两个核心/思想/模块:IOC/AOP
搭建框架项目:
1.导包 4(beans+core+context+expression)+2(log4j+logging)
2.JavaBean类
3.写配置文件 位置随意/名字随意
建议:src/applicationContext.xml
约束:schema(命名空间约束) - 加到根标签里的
beans aop tx mvc context
< bean class="" name="">
4.使用/开启Spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext(“applicationContext.xml”);
==千万记住new 出来的对象要加 配置文件 ==
Spring配置文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd ">
<!-- 管理对象 -->
<bean name="user" class="a_hello.User"></bean>
</beans>
package a_hello;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test01 {
@Test
public void test01() {
// 1.开启spring容器 - 加载配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2.索要对象
User user = (User) ac.getBean("user");
System.out.println(user);
}
}
实体类User
package a_hello;
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
}