Spring框架学习

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 + "]";
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值