Spring框架学习 - 配置
2013-06-17 13:33
471 查看
【资料】 ★★☆ Spring 中提供一些Aware相关接口,像是BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,实作这些 Aware接口的Bean在被初始之后,可以取得一些相对应的资源,例如实作BeanFactoryAware的Bean在初始后,Spring容器将会 注入BeanFactory的实例,而实作ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。
【资料】★★☆【通过ApplicationContextAware获取bean】
【资料】★★☆在非web应用的系统中怎样手动去销毁Spring容器
【资料】★★★★★ 详解 Spring 3.0 基于 Annotation 的依赖注入实现
-------------------------------------分割线 上面是转载的部分内容,下面是个人的学习总结---------------------------------
Spring框架在java中应用非常广泛。配制方法也有很多种。其本身在jsp项目中Myeclipse整合的很好。自动生成相对应的xml配置文件。
我自己学习,记录一下自己在java项目中使用Spring的配置
Spring框架提供了一个抽象接口ApplicationContextAware接口
接口内部有一个setApplicationContext()方法
在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的
public void setApplicationContext(ApplicationContext context) throws BeansException方法,获得ApplicationContext对象。
新建一个类 实现这个接口
在配置文件中添加下面一条。因为不需要任何其他属性,所以不需要写任何其他东西
这么配置之后。在代码中或者实际使用中。我们可以使用
来直接获取想要获取的bean了。
ApplicationContext 接口的最常用的实现类是 ClassPathXmlApplicationContext 和 FileSystemXmlApplicationContext,以及面向 Portlet 的 XmlPortletApplicationContext 和面向 web 的 XmlWebApplicationContext,它们都是面向 XML 的。
Spring 3.0 新增了另外两个实现类:AnnotationConfigApplicationContext 和 AnnotationConfigWebApplicationContext
详细内容可以直接参考五星级资料
配置的内容全部都在beans的xml文件中。
在web应用的项目中,如果项目服务关闭了,spring会自动销毁创建的容器,但是在非web应用的项目中,则需要我们手动去销毁。spring在AbstractApplicationContext中定义了一个registerShutdownHook()的方法,只要我们调用该方法就行了。
Spring 2.5 在 @Repository 的基础上增加了功能类似的额外三个注解:@Component、@Service、@Constroller,它们分别用于软件系统的不同层次:
@Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。
@Service 通常作用在业务层,但是目前该功能与 @Component 相同。
@Constroller 通常作用在控制层,但是目前该功能与 @Component 相同。
自动检索基础包dev.server之下的全部文件。
【资料】★★☆【通过ApplicationContextAware获取bean】
【资料】★★☆在非web应用的系统中怎样手动去销毁Spring容器
【资料】★★★★★ 详解 Spring 3.0 基于 Annotation 的依赖注入实现
-------------------------------------分割线 上面是转载的部分内容,下面是个人的学习总结---------------------------------
Spring框架在java中应用非常广泛。配制方法也有很多种。其本身在jsp项目中Myeclipse整合的很好。自动生成相对应的xml配置文件。
我自己学习,记录一下自己在java项目中使用Spring的配置
Spring框架提供了一个抽象接口ApplicationContextAware接口
接口内部有一个setApplicationContext()方法
在加载Spring配置文件时,会自动调用ApplicationContextAware 接口中的
public void setApplicationContext(ApplicationContext context) throws BeansException方法,获得ApplicationContext对象。
新建一个类 实现这个接口
import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class SpringContext implements ApplicationContextAware{ private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{ AppContext.applicationContext = applicationContext; } public static Object getBean(String beanName){ if (beanName == null) { return null; } return applicationContext.getBean(beanName); } }
在配置文件中添加下面一条。因为不需要任何其他属性,所以不需要写任何其他东西
<bean class="dev.server.config.spring.SpringContext"></bean>
这么配置之后。在代码中或者实际使用中。我们可以使用
SpringContext.getBean("contextName");
来直接获取想要获取的bean了。
ApplicationContext 接口的最常用的实现类是 ClassPathXmlApplicationContext 和 FileSystemXmlApplicationContext,以及面向 Portlet 的 XmlPortletApplicationContext 和面向 web 的 XmlWebApplicationContext,它们都是面向 XML 的。
Spring 3.0 新增了另外两个实现类:AnnotationConfigApplicationContext 和 AnnotationConfigWebApplicationContext
详细内容可以直接参考五星级资料
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class); ctx.registerShutdownHook();
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration @ImportResource("classpath:resource/beans.xml") public class SpringConfig { }
配置的内容全部都在beans的xml文件中。
在web应用的项目中,如果项目服务关闭了,spring会自动销毁创建的容器,但是在非web应用的项目中,则需要我们手动去销毁。spring在AbstractApplicationContext中定义了一个registerShutdownHook()的方法,只要我们调用该方法就行了。
Spring 2.5 在 @Repository 的基础上增加了功能类似的额外三个注解:@Component、@Service、@Constroller,它们分别用于软件系统的不同层次:
@Component 是一个泛化的概念,仅仅表示一个组件 (Bean) ,可以作用在任何层次。
@Service 通常作用在业务层,但是目前该功能与 @Component 相同。
@Constroller 通常作用在控制层,但是目前该功能与 @Component 相同。
自动检索基础包dev.server之下的全部文件。
<context:annotation-config /> <context:component-scan base-package="dev.server" />
相关文章推荐
- J2EE进阶学习——Spring框架(七):连接池配置和使用
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
- 17.笔记JAVA Spring框架学习————基于注解配置Bean之一
- Spring框架学习之注解配置与AOP思想
- spring框架的学习(二)——Spring的bean管理(xml配置文件方式)
- Spring框架学习(6)使用ioc注解方式配置bean
- Spring框架学习-深入理解AOP02----AOP简介,AspectJ,AOP基于注解和XML配置(5种通知,切面优先级)
- 码农小汪-spring框架学习之6-spring基于注解的容器配置 @Qualifier @Autowired @Resouce @PostConstruct @PreDestroy
- 18.笔记JAVASpring框架学习————基于注解配置Bean之二
- Spring框架学习之注解配置与AOP思想
- Spring框架学习之高级依赖关系配置(一)
- spring框架学习笔记2:配置详解
- Spring框架学习第一讲(IOC的XML配置文件方式)
- (学习笔记)spring框架入门之配置bean(处理属性中有集合的情况)
- Spring框架学习(2):基于全类名的方式配置bean
- Spring框架学习笔记----xml配置
- spring框架学习笔记3:使用注解代替配置文件
- Spring框架学习之高级依赖关系配置(二)
- Spring框架学习(1):环境配置以及HelloWorld
- Spring框架学习(3):spring属性配置的细节