Spring IOC/Bean

来源:互联网 发布:unity3d粒子特效教程 编辑:程序博客网 时间:2024/06/08 20:01

1、Spring框架的搭建?

A:创建Maven工程

B:转换为Web Project

C:添加基本Spring基本依赖库

Spring核心jar

org.springframework.asm-xx.RELEASE.jar

org.springframework.core-xx.RELEASE.jar

org.springframework.beans-xx.RELEASE.jar

org.springframework.context-xx.RELEASE.jar

org.springframework.expression-xx.RELEASE.ja

 

 

A:创建Maven工程

注:如果在创建过程中失败了就在C:\Users\Administrator\.m2删掉.m2后再次创建

 

aNew->Other->Maven->Maven Project->Next

 

 

bNext后出现下图,不做任何选择选择->Next

 

 

c:选择第一个->Next

 

 

dGroup id:类似包命名Artfact id:工程名->Finish->如果在工程的左上角有M代表就成功了。

 


B:转换为Web Project

a:选中刚创建成功的那个工程->右键->Maven->Update Project

 

b:选中你需要转换的工程->Force Update of snapshots/releases->Ok

 

 

cProject->Properties

 

 

dMaven-Project Facets->Convert to faceted from...

 

 

e:选择Dynamic Web Module->Runtimes->选中本地服务器->Apply->Ok

 

 

f:如下成功完成Maven->Web Project

 

 

 

C:添加基本Spring基本依赖库

注:如果在创建过程中失败了就在C:\Users\Administrator\.m2中删掉相应的依赖文件即可

 

a:在Maven仓库下载依赖http://mvnrepository.com/

->搜索Spring->选择你所需要的依赖

 

 

b:选择相应的版本

 

 

 

c:复制下方框里的代码

 

 

d:此时回到刚创建的工程->pom.xml->pom.xml

 

 

e:添加<dependencies>标签

 

 

 

f:将刚刚在Maven中复制的代码黏贴在<dependencies>标签中,然后保存

 

 

gMaven Dependencies中出现依赖就算成功

 

 

2、BeanIoc

Spring Ioc容器管理的组成我们应用程序中对象即BeanBean是由Spring容器初始化,配置和管理的对象。

Bean对象的两种模型:单态和原型模型

单态模型:提供了具有特定名称的对象的共享实例,可以在查询时对其进行检索。Singleton是默认的也是最常用的对象模型。对于无状态服务对象很理想。

原型模型:确保每次检索都会创建单独的对象。在每个用户都需要自己的对象时,原型模型最适合。

 

 

A:使用配置<bean>获取Bean

a:在刚才的工程中创建配置文件->applicationContext.xml

 

b:在applicatonContext.xml文件中写入如下代码

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

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-4.0.xsd   

        http://www.springframework.org/schema/context   

        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

</beans>

 

c:创建自己Bean

public class HelloWorld {

public void sayHello(){

System.out.println("Hello world!");

}

}

 

d:在配置文件applicationContext.xml中配置Beanid自己定义为你的类的唯一标识,class为你的类的路径(可以在你的类中,鼠标放在你的类名上直接复制路径)

 

 

e:创建测试类,完成测试

public class Test {

public static void main(String[] args) {

//BeanFactory接口提供了Ioc的基本功能

//ApplicationContext接口拓展了BeanFactory接口

ApplicationContext context =

new ClassPathXmlApplicationContext("applicationContext.xml");

//ClassPathXmlApplicationContext实现类从classpath中获取配置文件

//FileSystemXmlApplicationContext实现类从系统文件中获取配置文件

HelloWorld hw = context.getBean("hello",HelloWorld.class);

//hello是配置文件中你配置的id

hw.sayHello();

}

}

f:测试结果

 

 

B:使用注解获取Bean

 

a:给Bean一个注解

@Component("hello")

//@Component注解表示一个普通的Bean类,括号里可以给类起别名,这里给的是hello

public class HelloWorld {

public void sayHello(){

System.out.println("Hello world!");

}

}

 

b:在配置文件applicationContext.xml中指定扫描的区域

 

 

c:测试类与上面相同,这只是提供了两种方法供你选择。实际上它们的作用是一样的,看你喜欢用那种。

0 0
原创粉丝点击