博客开发笔记一——Spring3最小配置

选择Spring3作为基础框架开发博客似乎是正确的,因为回过头来看初学者入门要掌握的东西并不多,并且也很容易理解,通过搭建一个最小系统框架就可以说明这点:

一、首先配置web.xml

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
		classpath*:/applicationContext.xml
		classpath*:/applicationContext-security.xml
		classpath*:/applicationSecurityContext.xml
		classpath*:/applicationSecurity*.xml
	</param-value>
	</context-param>

 配置文件地址,可以配置多个文件,也可以使用通配符

<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

 监听器,加载环境

二、配置Spring Bean定义文件,文件名要符合web.xml中的配置

	<context:annotation-config />
	<context:component-scan base-package="mysite.security.services" />
	<context:component-scan base-package="mysite.*" />

 annotation-config说明可以使用注解,component-scan说明注解的代码在哪个包,系统到哪里去扫描这些注解

三、不相信吧,居然已经配置完成了,由于使用了注解方式,可以不在xml里面配置Spring Bean了,让我们来使用一下:

@Component
public class MyClass {
}

 通过@Compoent注解就可以将这个类发布成Spring Bean

@Autowired MyClass myInstance;

 使用@Autowired注解就将Bean注入到了类中

四、很简单吧,不超过四步就可以开发Spring程序了,虽然还有很多复杂特性,但通过这个最简系统应该可以入门了。

猜你喜欢

转载自tedeum.iteye.com/blog/1121470