spring导入约束及注册对象到容器
来源:互联网 发布:阿里云服务器开通端口 编辑:程序博客网 时间:2024/05/17 22:18
一、导入约束
在eclipse中:
window->preference
找到xml catalog
点击Add:
点击
File System添加spring的约束,这里以添加bean的约束为例,找到自己下载的
约束文件,如:
G:\Spring\spring-framework-4.2.4.RELEASE-dist\spring-framework-4.2.4.RELEASE\schema\beans
然后在spring的配置文件applicationContext.xml中添加beans标签
将applicationContext.xml的视图切换到design
在beans标签上右键选择 Namespace Declarations
点击add选择xsi
继续add点击 Specify New Namespace选择上面添加的spring beans的约束
bean的约束就导入成功了。
二、 注册bean对象到容器
1、新建一个bean
如:
package cn.pursue.bean;public class Person { private String name; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; }}
2、将对象注册到容器:
<?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.2.xsd "> <bean name="person" class="cn.pursue.bean.Person"></bean></beans>
3、测试是否注册成功:
package cn.pursue.demo;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.pursue.bean.Person;public class Demo01 { @Test public void test(){ //1 创建容器对象 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //2 向容器取出Person对象 Person person = (Person) ac.getBean("person"); //3 打印Person对象 System.out.println(person); }}
输出:cn.pursue.bean.Person@44c8afef
可见这个bean已经注册到spring的容器中了。
阅读全文
0 0
- spring导入约束及注册对象到容器
- Spring怎么导入约束
- AAAMybatis使用MapperScannerConfigurer实现自动注册dao到spring容器
- java将对象注册到spring context中
- 理解spring的BeanDefinition和配置文件(原文标题:动态注册bean到spring容器)
- 获取Spring容器对象
- Spring容器对象(BeanFactory)
- Spring容器对象(BeanFactory)
- spring容器 创建对象
- Spring容器管理对象
- Spring创建容器对象
- spring容器管理对象
- 手动将new出来的object注入(注册)到spring容器中
- SSM框架搭建及源码解析--spring容器Bean的注册(二)
- [图解] Spring导入约束的流程
- spring 导入到eclipse
- spring容器对象的生命周期
- 如何得到Spring 容器对象
- DOTween
- TP5 Validate 验证
- iOS 基于Open CV的边缘检测
- 利用GIT把项目push到GitHub上の简单示例
- jQuery+Ajax+jsonp+java实现跨域访问
- spring导入约束及注册对象到容器
- layer弹出ifream,提交之后自动关闭;无限级分类排序以及无限级分类子孙图树形展示
- 基本类型转换
- 素数
- hibernate主键生成策略
- 文本自动生成研究进展与趋势
- 关于互斥锁
- CentOS Linux搭建SVN服务器
- LintCode 第463题 整数排序