★★★Spring常用,不断更新...

一、Spring简介

Spring 是一个开源框架。

Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的JavaBean 实现以前只有 EJB 才能实现的功能。

Spring 是一个 IOC(DI) 和 AOP 容器框架。

二、配置bean

1、        eclipse上安装SPRING - TOOL- SUITE

SPRING TOOL SUITE 是一个 Eclipse 插件,利用该插件可以更方便的在 Eclipse 平台上开发基于 Spring 的应用。

安装方法说明(springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip):

a)      Help --> Install New Software...

b)      Click Add...

c)       In dialog Add Site dialog,click Archive...

d)      Navigate to springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip  and click Open

e)      Clicking OK in the AddSite dialog will bring you back to the dialog 'Install'

f)       Select the xxx/Spring IDEthat has appeared

g)      Click Next  and then Finish

h)      Approve the license

i)        Restart eclipse when that isasked

 

安装选取的时候,只选择带有“Spring IDE”的就够了。

2、 Spring环境配置

第一步,导入架包。

Spring需要的架包如下:


第二步,配置文件

配置文件约定俗称的名字叫:applicationContext.xml,内容可以从Spring的实例中拷贝出,也可以用以上的SPRING TOOL SUITE这个工具建立。

3、Spring如何应用在web应用中

步骤:

第一步、加入相应的web架包

spring-web-4.1.4.RELEASE.jar

spring-webmvc-4.1.4.RELEASE.jar

第二步、创建IOC容器

    我们知道,非web项目创建IOC容器(一般用ApplicationContext)是在main方法中创建,但是web项目创建IOC容器一般是在web服务器加载整个web项目的时候创建,这就需要用上ServletContextListener这个监听器。这个监听器会在web服务器一加载web项目的时候启动,contextInitialized()方法是这个监听器的主要方法。

在ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建 IOC 容器。在创建好IOC容器后,可以把其放在ServletContext(即application域)的一个属性中。

第三步、配置Spring 配置文件的名字和位置,以提高应用的可扩展性。一般将其配置到当前 WEB 应用的初始化参数中。

【实例】

监听器代码

public classSpringServletContextListener implements ServletContextListener {
 
   /**
     * Default constructor.
     */
   publicSpringServletContextListener() {
        // TODO Auto-generated constructor stub
   }
 
    /**
     * @seeServletContextListener#contextInitialized(ServletContextEvent)
     */
   publicvoidcontextInitialized(ServletContextEvent arg0) {
    //1. 获取 Spring 配置文件的名称.
    ServletContext servletContext =arg0.getServletContext();
    String config =servletContext.getInitParameter("configLocation");
   
    //1. 创建 IOC 容器
    ApplicationContext ctx = newClassPathXmlApplicationContext(config);
   
    //2. 把 IOC 容器放在 ServletContext 的一个属性中.
    servletContext.setAttribute("ApplicationContext",ctx);
   }
 
    /**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
   publicvoidcontextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
   }
}

web.xml代码

<context-param>
   <param-name>configLocation</param-name>
   <param-value>applicationContext.xml</param-value>
</context-param>
<!-- 启动IOC容器的ServletContextListner方法 -->
<listener>
   <listener-class>com.atguigu.spring.struts2.listeners.SpringServletContextListener</listener-class>
</listener>

 

对于上述过程,其实spring已经为我们准备好了一个自带的监听器ContextLoaderListener,在web.xml中可以直接创建这个监听器的配置,不用自己再重复建立一个监听器:

步骤如下:

第一步、在web.xml文件中配置ContextLoaderListener监听器。

<!-- needed forContextLoaderListener -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:ApplicationContext.xml</param-value>
</context-param>
 
<!-- Bootstraps the root webapplication context before servlet initialization -->
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener> 

第二步、在web应用中取出IOC容器。

// 从application域中得到IOC容器
ApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationCotext(application);

4、Spring如何集成Struts2

一般来说,struts的action都是由struts2自己创建的,整合spring和struts的目的就是将action交给spring管理,在访问action的时候从spring的IOC容器中获取,而不是让struts创建。

步骤:

第一步、加入架包

struts2-spring-plugin-2.3.16.3.jar

第二步:

将相应的action类配置成bean即交给spring管理。这里注意,需要配置scope属性为prototpe,不能采用默认的单例模式。

第三步、

<action name="personAction"class="com.spring.web.PersonAction">
    <result>/success.jsp</result>
</action>

这里class属性就不用写类的全类名了,只需要写配置在IOC容器中的bean的id即可。

5、Spring的声明式事物管理

<!-- 配置事物管理器,事物管理器依赖于数据源-->

<bean id=" transactionManager"

         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

         <propertyname="dataSource" ref="dataSource"></property>

</bean>

<!-- 配置事务属性 -->

<tx:advice id="txAdvice"transaction-manager="transactionManager">

         <tx:attributes>

                  <!--根据方法名指定事务的属性 -->

                  <tx:methodname="purchase" propagation="REQUIRES_NEW"/>

                  <tx:methodname="get*" read-only="true"/>

                  <tx:methodname="find*" read-only="true"/>

                  <tx:methodname="*"/>

         </tx:attributes>

</tx:advice>

<!-- 配置事物的切入点,以及把事物切入点和事物属性关联起来 -->

<aop:config>

         <aop:pointcutexpression="execution(* com.cll.spring.tx.xml.service.*.*(..))"

         id="txPointCut"/>

         <aop:advisoradvice-ref="txAdvice" pointcut-ref="txPointCut"/>

</aop:config>

更加详细的事物管理配置:http://blog.csdn.net/it_man/article/details/5074371

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 如果你的Spring Boot项目中出现了注解错误不断的情况,可能有几个原因导致。首先,你需要确保你的项目中正确使用了zuul路由的注解,即在启动类上添加了@EnableZuulProxy注解\[1\]。其次,你需要检查是否正确使用了Spring Cloud相关的注解,包括@SpringCloudApplication、@EnableDiscoveryClient和@EnableCircuitBreaker\[2\]。另外,如果你在项目中使用了@ConfigurationProperties注解,你需要确保在pom.xml文件中添加了相应的依赖,如spring-boot-configuration-processor\[3\]。如果你仍然遇到注解错误,建议检查你的代码是否正确引入了相关的依赖,并且按照正确的方式使用了注解。 #### 引用[.reference_title] - *1* *2* [Spring Boot常用注解(绝对经典)](https://blog.csdn.net/guorui_java/article/details/107379648)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [idea提示未配置 Spring Boot 配置注解处理器解决方法](https://blog.csdn.net/m0_67393827/article/details/126721244)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值