spring4.0学习笔记(一)

spring两大特征:

1、IOC控制反转(DI依赖注入)

2、AOP面向切面编程

eclipse安装spring插件

1、官网下载 springsource-tool-suite-3.9.4.RELEASE-e4.6.3-updatesite.zip(我本机下载)

官网地址:http://spring.io/tools/sts/all

2、eclipse安装

3,、导包

4、小demo

public static void main(String[] args) {

// HelloWorld helloWorld = new HelloWorld();
// helloWorld.setUser("Tom");
// helloWorld.hello();

//1. 创建 Spring 的 IOC 容器 把对象都交给IOC容器管理,ClassPathXmlApplicationContext获取容器中的对象。
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

//2. 从 IOC 容器中获取 bean 的实例
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");     getBean("helloWorld")==配置文件里的  <bean id="helloWorld".../>

//根据类型来获取 bean 的实例: 要求在 IOC 容器中只有一个与之类型匹配的 bean, 若有多个则会抛出异常.
//一般情况下, 该方法可用, 因为一般情况下, 在一个 IOC 容器中一个类型对应的 bean 也只有一个.
// HelloWorld helloWorld1 = ctx.getBean(HelloWorld.class);

//3. 使用 bean
helloWorld.hello();

配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<!-- 配置bean属性   -->
<bean id="helloWorld" class="com.yjxc.spring.beans.HelloWorld"></bean>
<property name="name" value="Spring"></property>

</beans>

猜你喜欢

转载自www.cnblogs.com/chenxiaoxu/p/9180979.html