Spring 4.1.6
一、下载
Spring下载搞得很麻烦啊!!(鼓励使用maven管理,看来以后也得学习下怎么使用)
1、官网:
2、下载地址:
3、下载帮助:
http://www.cnblogs.com/yjmyzz/p/3847364.html
http://jingyan.baidu.com/article/a65957f49670ac24e67f9b85.html二、配置:
需要commons logging与log4j日志支持。
1、下载日志jar包
commons logging:
http://commons.apache.org/proper/commons-logging/download_logging.cgi
log4j:
http://logging.apache.org/log4j/1.2/2、配置
2.1 导入jar包
将Spring解压后的libs目录下的类似spring-*-4.1.6.RELEASE.jar的文件拷贝到当前项目WEB-INF/lib目录下;
将commons logging目录下的commons-logging-1.2.jar文件拷贝到当前项目WEB-INF/lib目录下;将log4j目录下的log4j-1.2.17.jar文件拷贝到当前项目WEB-INF/lib目录下;
2.2 配置日志文件
src目录下创建log4j.properties文件(这样不用添加路径,算是取巧吧),初始内容为(不固定,自己配置时使用下面的内容就成功了):
# 输出级别,输出错误信息,输出源为标准输出源stdout
log4j.rootLogger=WARN, stdout
# 将stdout输出到控制台中
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# 日志输出的布局类
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# 指定日志输出内容的格式
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
注意:
(1)后缀名为properties的配置文件在eclipse中查看时中文会为乱码或十六进制码,解决方法参考
(2)插件的安装方法
http://www.cnblogs.com/kevinkim/archive/2012/11/09/2762112.html
(3)Eclipse外部jar包导入Eclipse - http://blog.csdn.net/mazhaojuan/article/details/21403717
Eclipse EE - 直接拷贝到WEB-INF/lib文件夹下即可,会自动作为Web App Libraries
(4)关于配置文件的路径(classpath)说明
http://blog.csdn.net/haohaodigua/article/details/17582877
三、测试
Inversion of Control (IoC) = Dependency injection (DI)
1. IoC container
说明:配置文件applicationContext.xml文件也放置在src目录下,与log4j.properties相同
(1) 通过BeanFactory获取Bean(不推荐使用)
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
User user = (User)factory.getBean("user"); //此处user为Bean的属性id的值,即对象User的实例
(2) ApplicationContext接口继承于BeanFactory,类 ClassPathXmlApplicationContext、 FileSystemXmlApplicationContext、WebApplicationContext分别实现了此接口,因此可以通过ApplicationContext获取Bean
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User)context.getBean("user", User.class); <span style="font-family: Arial, Helvetica, sans-serif;">//此处user为Bean的属性id的值,即对象User的实例</span>
(3) Web程序中在web.xml配置后不需要实例化Container,配置如下:
<pre name="code" class="html"> <servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<span style="font-family: Arial, Helvetica, sans-serif;"> </span><span style="font-family: Arial, Helvetica, sans-serif;"><!--此处也可以使用</span><span style="font-family: Arial, Helvetica, sans-serif;">classpath:applicationContext.xml或绝对路径,能找到即可--!></span>
<param-value>classpath*:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
2、 Bean与Spring的依赖注入
(1) Setter注入<bean id="user" class="nautilus.study.spring.javabean.User">
<property name="name">
<value>小明</value>
</property>
<property name="age">
<value>25</value>
</property>
<property name="sex">
<value>男</value>
</property>
</bean>
(2) Constructor注入
<bean id="user2" class="nautilus.study.spring.javabean.User">
<constructor-arg>
<value>小明</value>
</constructor-arg>
<constructor-arg>
<value>男</value>
</constructor-arg>
<constructor-arg>
<value>25</value>
</constructor-arg>
</bean>