java-Spring 管理bean例子
Spring 通过2种方式管理bean
首先要导入Spring的包,(Spring.jar和commonslogging.jar) 或加载分开的...
在src目录下建立applicationContext.xml (Spring 管理 bean的配置文件)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPEING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="user" class="com.cc8w.entiey.User"/>
</beans>
1.使用BeanFactory管理bean
2.使用ApplicationContext管理bean
package com.cc8w; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import com.cc8w.entiey.User; public class TestBean { public static void main(String[] args) { //BeanFactory 管理bean
Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory =new XmlBeanFactory(resource);
User user = (User) factory.getBean("user");
System.out.println(user); //applicationContext 管理bean
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user1 = (User)context.getBean("user");
System.out.println(user1); } }
java的数据类型
package com.cc8w.entiey;
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
结果:

呃.. 写错了,应该时entity.
Spring 控制反转 依赖注入 (配置初始化参数-实例化参数)
1.接口注入
2.Setter注入(推荐)
3.构造器注入
java-Spring 管理bean例子的更多相关文章
- (转)编码剖析Spring管理Bean的原理
http://blog.csdn.net/yerenyuan_pku/article/details/52832434 在Spring的第一个案例中,我们已经知道了怎么将bean交给Spring容器进 ...
- 采用Spring管理Bean和依赖注入
1. 实例化spring容器和从容器获取Bean对象 实例化Spring容器常用的两种方式: 方法一: 在类路径下寻找配置文件来实例化容器 [推荐使用] ApplicationContext ctx ...
- Spring、编码剖析Spring管理Bean的原理
引入dom4j jar包 1.新建Person接口和PersonBean public interface PersonIService { public void helloSpring(); } ...
- Spring第三弹—–编码剖析Spring管理Bean的原理
先附一下编写的Spring容器的执行结果: 代码如下: 模拟的Spring容器类: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- 编码剖析Spring管理bean的原理
project目录 MyClassPathXMLApplicationContext读取xml,以及实例化bean. 因为是一开始实例化配置文件所有bean,所以需要构造器完成这些工作. packag ...
- Spring管理bean的生命周期
1: bean的创建: 如果我们默认的scope配置为Singleton的话, bean的创建实在Spring容器创建的时候创建: 如果scope的配置为Prototype的话,bena的创建是在 ...
- 简单模拟Spring管理Bean对象
1: 首先我们要利用dom4j进行xml的解析,将所有的bean的配置读取出来. 2:利用java的反射机制进行对象的实例化. 3: 直接获得对象 package cn.Junit.test; imp ...
- java——spring中bean的作用域
文章:理解Spring框架中Bean的作用域 博客地址:https://baijiahao.baidu.com/s?id=1610298792072480906&wfr=spider& ...
- spring 管理bean
目录结构: Person.java package com.wss.entity; import com.wss.service.doHomeWork; public class Person { p ...
随机推荐
- Linux文件与目录操作
1:目录操作指令 cd :切换目录 pwd:显示当前目录 mkdir:创建一个新目录 rmdir:删除一个空的目录rmdir -r:删除一个非空目录 . :此层目录 .. :上层目录 -:前一个工作目 ...
- The cast to value type 'System.Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
CurrentStock = db.BillEntry.Where(b => b.GoodsId == item.GoodsId).Sum(b => (decimal?)b.Qty) ?? ...
- coco2d-js 触摸移动的同时处理点击事件
cc.eventManager.addListener({ event: cc.EventListener.TOUCH_ALL_AT_ONCE, onTouchesMoved:function (to ...
- Java AtomicBoolean (Java代码实战-008)
值得一提的是,Java的AtomXXX类并不是使用了锁的方式进行同步,而是采用了一种新的理念,叫做CAS(Compare And Swap)CAS是一组CPU原语指令,用来实现多线程下的变量同步(原子 ...
- [Nginx]用Nginx实现与应用结合的訪问控制 - 防盗链
应用场景:图片等资源须要设置权限,如:仅仅有认证过的用户才干訪问自己的图片. 解决的方法:使用Nginx的防盗链模块http_secure_link能够实现,该模块默认情况下不包括.故在安装时要加上- ...
- 【DB2】数据迁移
数据迁移概述 在日常生活中常有数据的导入导出,为此db2提出了很多工具可以选择,export.import.load.db2look.db2move.db2dart,如下图所示: a.最上面虚线框部分 ...
- LR函数基础(一)(二)
LR函数基础(一) 函数用到:web_reg_find(). lr_log_message(). lr_eval_string().strcmp().atoi() Action(){ web_r ...
- mysql8.x开启远程登录
mysql8.x密码的认证方式变了,需要修改远程认证方式 问题描述 Connection to lab-130-mysql failed.Unable to load authentication p ...
- Ubuntu与Windows7双系统下, 系统时间不一致的问题
Ubuntu使用的UTC时间, 而Windows使用的是Local Time, 就导致每次切换系统后, Windows时间都会正好晚8个小时. 有两种解决办法, 一个是修改Ubuntu, 另一个是修改 ...
- numpy文件读写的三对函数
在Python很多库中,使用文件名的地方都可以使用文件对象来替代. 在下述三种方法中,都是如此. 一.a.tofile()和np.fromfile() numpy中的ndarray对象有一个函数tof ...