Spring 快速开始 配置Spring Framework

【配置Spring Framework】

1.XML配置依赖关系

bean是由Springframework管理,我们自己编写bean,Spring也内建了bean,比如ApplicationContext、ResourceLoader、BeanFactory、MessageSource、ApplicationEventPublisher。

【Servle级别上下文】/WEB-INF/servletContext.xml     

<beans>

  <mvc:anntation-driven />     <!-- 作用是 指示Spring使用@RequestMapping @RequestBody @RequestParam @PathParam @ResponseBody --->

  <bean name="greetingServiceImpl" class="com.wrox.GreetingServiceImpl" />

  <bean name="helloController" class="com.wrox.GreetingServiceImpl" >

    <property name="greetingService" ref="greetingServiceImpl" />

  </bean>

</beans>

 

【@RequestMapping细节】

在声明了DispatcherServlet后,@RequestMapping是相对于Dispatcher的URL-pattern而不一定是web程序根URL。

【上下文细节】

一般的程序都有两个上下文,【根上下文用来容纳业务逻辑类】+【Servlet级别上下文用来容纳控制器类】

【根上下文级别】/WEB-INF/rootContext.xml   声明的bean ,DispatherServlet的应用上下文也会继承所有根上下文的bean。

<beans>

  <bean name="greetingServiceImpl" class="com.wrox.GreetingServiceImpl" / >

</beans>

然后在web.xml部署描述符

<context-param>

  <param-name>contextConfigLocation</param-name>

  <param-value>/WEB-INF/rootContext.xml</param-value>

</context-param>

<listener>

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

</listener>

【Spring日志细节】log4j-jcl 支持Spring用Commons Logging打印日志到Log4j。

 

……………………………………………………………………………………………………………………………………………………………………………………………………

2.混合配置依赖关系 =  组件扫描 + 注解配置。

@Component  组件注解 ,@Controller @Repository @Service   + @AutoWired

【上下文细节目标】根上下文容纳Service、Repository和其他业务逻辑,DispatcherServlet上下文容纳Controller。

/WEB-INF/rootContext.xml 【黑名单】

<beans>

  <context:annotation-config />

  <context:component-scan base-package="com.wrox" >

    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

  </context-component-scan>

</beans>

/WEB-INF/servletContext.xml 【白名单】

<beans>

  <mvc:annotation-driven />

  <context:annotation-config />

  <context:component-scan base-package="com.wrox" use-default-filters="false" />

    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />

  </context:component-scan>

</beans>

【组件扫描不到的bean】第三方已经编译成Class无法通过添加注解 都可以在配置中 声明bean补充组件扫描扫不到的地方。

……………………………………………………………………………………………………………………………………………………………………

【使用@Configuration配置Spring】XML难于调试、XML无法单元测试(只能集成测试)。   +  @Inject

1.引入外部Properties

@Configuration 

@PropertySource({("classpath:com/wrox/config/settings.properties","file:config.properties")})

public class ExampleConfiguration{

  //如果需要访问properties的值

  @Inject Environment environment;

  @Value("my.property.key") String myPropertyValue;  //自动注入

}

2.拆分多个配置

@Configuration

@Import({DatabaseConfiguration.class,ClusterConfiguration.class})

@ImportResource("classpath:com/wrox/config/spring-security.xml")

public class ExampleConfiguration

 

@Configuration
@ComponentScan(
        basePackages = "com.wrox.site",
        excludeFilters = @ComponentScan.Filter(Controller.class)
)
public class RootContextConfiguration
{
}
@Configuration
@EnableWebMvc   
@ComponentScan(
        basePackages = "com.wrox.site",
        useDefaultFilters = false,
        includeFilters = @ComponentScan.Filter(Controller.class)
)
public class ServletContextConfiguration
{
}
public class Bootstrap implements WebApplicationInitializer
{
    @Override
    public void onStartup(ServletContext container) throws ServletException
    {
        //静态资源
        container.getServletRegistration("default").addMapping("/resource/*");

        AnnotationConfigWebApplicationContext rootContext =
                new AnnotationConfigWebApplicationContext();
        rootContext.register(RootContextConfiguration.class);
        container.addListener(new ContextLoaderListener(rootContext));

        AnnotationConfigWebApplicationContext servletContext =
                new AnnotationConfigWebApplicationContext();
        servletContext.register(ServletContextConfiguration.class);
        ServletRegistration.Dynamic dispatcher = container.addServlet(
                "springDispatcher", new DispatcherServlet(servletContext)
        );
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

 

转载于:https://www.cnblogs.com/chenhui7373/p/8687781.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值