spring-入门

导读:

    纵观这段时间的学习进程,突然发现自己好像变懒了,都没抽出时间将所学的知识进行归纳汇总,也许是因为一直着手自己的毕设项目上面。。。

    接下来就通过以前做过的笔记,代码来熟悉框架学习的相关知识。

1.spring是什么?

  spring是一个轻量型的框架,主要体现在管理每一个Bean组件的生命周期,不同Bean组件之间依赖关系上面。

  它主要是通过顶层容器BeanFactory来管理每一个Bean组件的生命周期,通过子类ApplicationContext实现工厂模式下每一个Bean组件的创建。

2.两大核心:

  IOC/DI:控制反转/依赖注入,service层注入dao层,action层(controller)注入service层

  AOP:面向切面编程

3.控制反转的作用:

  将对象的创建通过配置文件来声明,通过spring提供的容器进行创建,创建的bean对象为单例对象,同时通过配置文件或者注解来描述不同Bean组件之间的依赖关系。

4.依赖注入的实现方式:

1) 构造器注入:

 <bean id="constructor" class="com.wsw.entity.User">
        <constructor-arg name="" value="" ref=""/>
        <constructor-arg name="" value="" ref=""/>
    </bean>

2)传值注入/getter,setter注入:

  <bean id="constructor" class="com.wsw.entity.User">
        <property name="" value=""/>
        <property name="" value=""/>
        <property name="" value=""/>
    </bean>

3)接口注入

5.AOP的实现原理,实现方式:

  实现原理:

 1)jdk动态代理:仅支持接口的注入

 2)cglib植入:支持普通类的注入

  实现方式:

 1)通过配置文件进行设置:

<aop:config proxy-target-class="true">
<aop:pointcut id="pc" expression="execution(* com.wsw.service..*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
<aop:aspect>
<aop:before method="before" pointcut-ref="pc"/>
<aop:after method="after" pointcut-ref="pc"/>
<aop:around method="around" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>

2)通过注解@Aspect实现

 1 @Aspect
 2 public class HouseService {
 3 
 4     @Before("execution(* com.wsw.service..*(..)))")
 5     public void before() {
 6         System.out.println("before");
 7     }
 8 
 9     @After("execution(* com.wsw.service..*(..)))")
10     public void after() {
11         System.out.println("after");
12     }
13 
14 }

6.spring的事务:

1)编程式事务:通过编程的方式实现事务,具有极大的灵活性,但是难以维护。

2)声明式事务:可以将业务代码和事务管理分离,使用注解或者配置文件来管理事务。

7.spring的常用注解:

@Controller:

@Controller
public class UserController {}

@Service:

@Service
public class UserService {}

@Component:

@Component
public class House {}

@Autowired:

@Controller
public class HouseController {
    @Autowired
    private HouseService houseService;
    @Autowired
    private UserService userService;
}

@Resource:

public class UnitTest extends BaseTest {
    @Resource
    private UserDao userDao;
}

@Transactional:

@Transactional
    public ModelAndView houseManage(ModelAndView modelAndView, @RequestParam(defaultValue = "1", required = true, value = "pageNo") Integer pageNo) {
        PageHelper.startPage(pageNo, 8);
        List<House> list = houseService.showAll();
        PageInfo<House> pageInfo = new PageInfo<>(list);
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.setViewName("guanli");
        modelAndView.addObject("house", list);
        return modelAndView;
    }

8.spring的优缺点:

优点:降低了对象之间的耦合度,使代码更加清晰,并且能够提供一些常见的模板。

缺点:内容广泛比较难理解,需要不断地深入每一块的学习才能真正掌握(个人理解)

9.spring框架中能够体现哪些设计模式?

1)工厂模式:主要体现在每一个bean组件的创建过程

2)代理模式:主要体现在面向切面编程aop

3)单例模式:主要体现在每一个bean组件创建后的状态

10.spring框架中的配置内容主要有哪些?

1)开启注解功能,配置扫描包

    <context:annotation-config/>
    <context:component-scan base-package="com.wsw.*"/>

2)数据源的配置

  <!-- 配置数据源 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <bean id="dataSource" class="${dataSource}" destroy-method="close">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

3)会话工厂配置

  <!-- 配置sqlSessionFactory,SqlSessionFactoryBean是用来产生sqlSessionFactory的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 加载mybatis的全局配置文件,放在classpath下的mybatis文件夹中 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- 加载数据源,使用上面配置好的数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.wsw.entity"/>
        <property name="mapperLocations" value="classpath*:mapper/*-mapper.xml"/>
    </bean>

4)事务管理配置

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

5)aop配置

<aop:config proxy-target-class="true">
        <aop:pointcut id="pc" expression="execution(* com.wsw.service..*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
        <aop:aspect>
            <aop:before method="before" pointcut-ref="pc"/>
            <aop:after method="after" pointcut-ref="pc"/>
            <aop:around method="around" pointcut-ref="pc"/>
        </aop:aspect>
    </aop:config>
 
 

 

 

文章待完善。。。

 

转载于:https://www.cnblogs.com/weekstart/p/spring.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值