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后再次创建
a:New->Other->Maven->Maven Project->Next
b:Next后出现下图,不做任何选择选择->Next
c:选择第一个->Next
d:Group id:类似包命名Artfact id:工程名->Finish->如果在工程的左上角有M代表就成功了。
B:转换为Web Project
a:选中刚创建成功的那个工程->右键->Maven->Update Project
b:选中你需要转换的工程->Force Update of snapshots/releases->Ok
c:Project->Properties
d:Maven-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>标签中,然后保存
g:Maven Dependencies中出现依赖就算成功
2、Bean和Ioc
由Spring Ioc容器管理的组成我们应用程序中对象即Bean,Bean是由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中配置Bean。id自己定义为你的类的唯一标识,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:测试类与上面相同,这只是提供了两种方法供你选择。实际上它们的作用是一样的,看你喜欢用那种。
- spring IOC装配bean
- Spring IOC/Bean
- spring Ioc & Bean
- Spring Ioc装配Bean
- Spring IOC Bean life cycle
- 配置Spring Ioc中的bean
- Spring IOC--Bean的装配
- JavaWeb-Spring框架-IOC-Bean
- Spring IOC 源码-bean解析
- Spring IOC源码-bean加载
- spring IOC bean配置方法
- Spring--IOC及Bean容器
- Spring IOC 源码-bean解析
- spring-IOC容器bean生命周期
- Spring IOC 抽象bean设置
- spring ioc 容器装配bean
- Spring 1 IoC 2 Bean管理
- Spring 3 Ioc 容器装在bean
- iOS 关闭textField的粘贴 选择 全选等对内容操作的菜单
- matlab-cat函数
- JSP学习第二天--PPT演示
- 保存时检测窗体上控件中的数据是否发生变化 asp
- html 绘制太极
- Spring IOC/Bean
- map 中的坑
- 我来写博客
- python项目打包成pip安装&pip install 介绍
- Problem A: [noip2016十连测第五场]simple(模拟)
- 开源备忘
- 50. 避免重定向(11)
- 大漠的告白
- Android 自定义属性 attrs.xml