小生初出程序茅庐,走上编程之路,还请各位大佬多多管照。spring
初学Spring框架:express
建立HelloWorldSpring项目编程
用eclipse开发Web项目,添加jar包:app
commons-logging-1.2.jar框架
log4j-1.2.17.jareclipse
spring-beans-3.2.13.RELEASE.jar测试
spring-context-3.2.13RELEASE.jar3d
spring-core-3.2.13.RELEASE.jarxml
spring-expression-3.2.13.RELEASE.jar对象
spring配置:applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd">
<!--配置bean -->
<bean id="HellowSpring" class="com.projo.bean.Hellow">
<!--声名属性并赋值至关于setName("张三") -->
<property name="who">
<value>张三</value>
</property>
</bean>
</beans>
测试:
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
HelloSpring hellowSpring=(HellowSpring)context.getBean(applicationContext.xml);
注:
使用id属性为其指定一个用来访问的惟一的名称,若是想为Bean指定更多的别名,可经过name属性指定,名称之间使用逗号,分号,或者空格进行分隔。Spring为bean的属性赋值是经过调用属性的set方法实行的,这种作法称为“设值注入”。
写法1:
<property name="who">
<value>张三</value>
</property>
写法2:
<property name="who" value="张三"/>
ApplicationContext是一个接口。负责读取spring配置文件,管理对象的加载,生成,维护Bean与Bean之间的依赖关系,负责Bean的生命周期,ClassPathApplicationContext是ApplicationContext接口的实现类,用于classPath路径读取spring配置文件。而ApplicationContext是BeanFactory的子接口。BeanFactory是spring IoC容器的核心。负责管理组件和他们之间的依赖关系,应用程序经过BeanFactory接口与spring IoC容器交互。
“控制反转”:我的如今的理解是:
就是再也不用new去建立对象,而是把这一个控制权交给了SpringFactoryBean经过配置文件的方式去完成 。
jar包配置合适: