Spring_Point1

本文详细介绍了Spring框架的配置方式,包括注解配置、XML配置等,并深入探讨了@Service、@Autowired等注解的作用及使用场景,同时讲解了Spring与MyBatis、Struts2的整合方法。

配置文件的命名空间

 

 

 

Spring的注解机制,是类加载时读入配置文件的,即修改了关于注解的配置,如果不重启服务,那么配置文件就不会读取到注解的更新

 

@Service("serviceUser")

相当于 <bean id="serviceUser" class="com.yg.testdangdang.service.impl.ServiceUserImpl"></bean>

 

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

ServiceUser serviceUser  = (ServiceUser)context.getBean("serviceUser");

Spring创建的对象,才可以享有Spring的功能,比如自动装配的对象,如果new了一个新对象,那么就没有自动装配的功能了

 

BaseJunit4Test

@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试 

@ContextConfiguration(locations={"classpath:applicationContext.xml"})//加载配置文件

public class test {

 

   @Autowired

   private UserService userService;

  

   @Test

   public void test(){

      System.out.println(userService);

   }

}

 

<aop:pointcut expression="@annotation(com.yg.testdangdang.annotation.Log)" id="myPointcut"/>

定义切入点为注解,Log,需要提前创建这个软接口

@Target({ElementType.PARAMETER,ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

public @interface Log {

}

<aop:advisor advice-ref="myBeforeAdvice" pointcut-ref="myPointcut"/>

@Log 即有这个注解的,才实现myPointcut增强,

public boolean login(String userEmail,String userPassword) {}

 

@RequestMapping("/user")

public class UserController

在类名上面加RequestMapping注解,表示该类添加了命名空间。即所有的返回值加“/user”

 

return "forward:/main/main.jsp"

如果其中一个想例外,那么就要加forward(转发),redirect(重定向)关键字,表示不经过逻辑视图,并且前面要加一个“/”,来组合成绝对路径

 

Spring的配置文件,扫描路径是包路径,不是类路径

 

Dao层:

       <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

              <property name="basePackage" value="com.yg.testcmfz.dao"></property>

       </bean>

Service层:

<context:component-scan base-package="com.yg.testcmfz.service"></context:component-scan>

@Service配置在实现类,base-package指定接口的包路径

 

Spring+Struts2+MyBatis 整合

原在controller层使用SpringMVC的注解来提供service对此昂,现在由于使用了struts2,没有了注解了,那么就将url功能放在applicationContext上实现bean的创建,bean指出service的全路径,bean ID交给struts.xml class

<action name="*Book" class="bookAction" method="{1}">

整合后,Spring来创建对象,Struts2来控制流程的跳转

 

 

 

applicationContext配置文件bean的调用流:

没有了springMVC的注解,userAction要持有userService对象的引用

 

userAction设置userServiceget,set方法

 

userService要持有userDao对象的引用

 

userServiceImpl设置userDaoget,set方法

 

 

 

由于dao层是接口,因此需要添加mapperInterface参数,value制定dao层的实际位置;

整合MyBatis,需要持有sqlSessionFactory的引用

 

sqlSessionFactory持有dataSource的引用,并配置指明*DaoMapper.xml的实际路径

 

dataSource配置数据源

 

userActiondataSourcebean被层层调用,实际底层是靠:

  1. Set,Get 调用对象
  2. 无参构造方法和反射创建对象

 

assertNotNull 断言

  1. public User getUser(String username) {  
  2.     if (username == null || username.trim().length() != 0)  
  3.             throw new IllegalArgumentException("username is invalid,invalid username:" + username);  
  4.         //.......省去  
  5.     }

=

  1. public int getUser(String username) {  
  2.     Assert.hasText(username);//这里没有使用自定义的错误描述,而是使用默认的错误描述,  
  3. }

 

 

 

select d1.category_id,d1.category_name,d1.parent_id,d1.count,

                            d2.category_id category_id2,d2.category_name category_name2,d2.parent_id parent_id,d2.count count2 from

                             dangstore_category d1 inner join dangstore_category d2

                             on d1.category_id = d2.parent_id

使用内连接时,在select后需要给别名,不然java无法分辨正确的字段名,不可以使用d1.*,d2.*

 

Web.xml 加载顺序

java web规范中说明.按照listner,filter,servlet的顺序,从上至下初始化.

 

SpringMVC 异常处理

http://blog.youkuaiyun.com/goodyuedandan/article/details/62420120

推荐使用 实现HandlerExceptionResolver 接口自定义异常处理器

Controller方法内的try捕获异常 优于 全局异常捕获

 

public class MyExceptionResolver implements HandlerExceptionResolver{

 

       @Override

       public ModelAndView resolveException(HttpServletRequest req,

                     HttpServletResponse resp, Object o, Exception ex) {

              //Object是抛出异常的引发者,调用方法

              //Exception就是那个剖出的异常

              System.out.println("全局捕获异常");

              //判断这个异常属于那个异常类

              if (ex instanceof UnauthorizedException){

                     //打印异常内容信息

                     System.out.println(ex);

              }

              //向前台返回错误信息

        ModelAndView modelAndView = new ModelAndView();

        //将错误信息存入session

        modelAndView.addObject("error_message", ex);

        //跳转页面

        modelAndView.setViewName("/jsp/error");

              return modelAndView;

       }

}

 

 

spring.shardingsphere.sharding.tables.sale_commission_base_origin.actual-data-nodes=ds_master.sale_commission_base_origin,ds_slave_1.sale_commission_base_origin,ds_slave_2.sale_commission_base_origin spring.shardingsphere.sharding.tables.sale_commission_base_origin.table-strategy.standard.sharding-column=sales_date spring.shardingsphere.sharding.tables.sale_commission_base_origin.table-strategy.standard.sharding-algorithm-name=salesDateAlgorithm spring.shardingsphere.sharding.tables.sale_commission_calculated.actual-data-nodes=ds_master.sale_commission_calculated,ds_slave_1.sale_commission_calculated,ds_slave_2.sale_commission_calculated spring.shardingsphere.sharding.tables.sale_commission_calculated.table-strategy.standard.sharding-column=sales_date spring.shardingsphere.sharding.tables.sale_commission_calculated.table-strategy.standard.sharding-algorithm-name=salesDateAlgorithm spring.shardingsphere.sharding.tables.sale_commission_base_extension.actual-data-nodes=ds_master.sale_commission_base_extension,ds_slave_1.sale_commission_base_extension,ds_slave_2.sale_commission_base_extension spring.shardingsphere.sharding.tables.sale_commission_base_extension.table-strategy.standard.sharding-column=sales_date spring.shardingsphere.sharding.tables.sale_commission_base_extension.table-strategy.standard.sharding-algorithm-name=salesDateAlgorithm spring.shardingsphere.sharding.tables.sales_point_base_origin.actual-data-nodes=ds_master.sales_point_base_origin,ds_slave_1.sales_point_base_origin,ds_slave_2.sales_point_base_origin spring.shardingsphere.sharding.tables.sales_point_base_origin.table-strategy.standard.sharding-column=sales_date spring.shardingsphere.sharding.tables.sales_point_base_origin.table-strategy.standard.sharding-algorithm-name=salesDateAlgorithm spring.shardingsphere.sharding.tables.sales_point_calculated.actual-data-nodes=ds_master.sales_point_calculated,ds_slave_1.sales_point_calculated,ds_slave_2.sales_point_calculated spring.shardingsphere.sharding.tables.sales_point_calculated.table-strategy.standard.sharding-column=sales_date spring.shardingsphere.sharding.tables.sales_point_calculated.table-strategy.standard.sharding-algorithm-name=salesDateAlgorithm spring.shardingsphere.sharding.tables.sales_point_base_extension.actual-data-nodes=ds_master.sales_point_base_extension,ds_slave_1.sales_point_base_extension,ds_slave_2.sales_point_base_extension spring.shardingsphere.sharding.tables.sales_point_base_extension.table-strategy.standard.sharding-column=sales_date spring.shardingsphere.sharding.tables.sales_point_base_extension.table-strategy.standard.sharding-algorithm-name=salesDateAlgorithm // 这些配置对应哪段sharding代码
08-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值