spring常用的注解配置及Junit集成整合

前提提示:

小编在总结spring常用注解的前提已经对ssm框架有过整体的学习,这是对spring框架的深入理解,其中框架的中点是源码分析,不需要你自己写源码,主要是自己可以看懂,小编也没有提到AOP,IOC,JdbcTemplate,事务等部分总结,小编后面也会有相应的博客总结

整体总结:

 spring框架:AOP 和 IOC(DI)
         优势:解除耦合,开源代码
            面向切面编程
            支持事务管理
              测试更加方便
              结合其他框架
  注意的注解的方式注入对象:(xml配置文件创建对象在实际开发不常用)
                Bean对象的注创建:@Service(service)  @@Repository(dao)  @Controller(web)  @Component(使用较少)
                       属性注入:@Value(普通属性) @Autowired(其他属性:对象类等属性)
                Spring新注解:支持注解类取代配置文件 @Configuration   @ComponentScan("Pakage name")   @ProperSource("properties")
                            但是spring实际xml文件用的更多 springboot使用的较多
                spring集成junit:@RunWith(SpringJUnit4ClassRunner.class)      @ContextConfiguration("classpath:spring.xml")

对应xml创建Bea对象了解知道怎么创建即可,对于全注解类spring框架实际使用不多,而springboot实现的较多,小编在后面的学习中也会更新,现在小编还不知道springboot那是啥子玩意儿.....

简单的实现:

实体类:

package com.adtchengdu.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.validation.annotation.Validated;

import java.util.List;

@Repository
public class User {
    @Value("1")
    private Integer id;
    @Value("张三")
    private String name;
    @Value("20")
    private  Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }


    public User() {
    }

    public User(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

接口以及实现类: 

package com.adtchengdu.Dao;

public interface UserDao {
    public void save();
}


package com.adtchengdu.Dao.impl;

import com.adtchengdu.Dao.UserDao;
import com.adtchengdu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoimpl implements UserDao {

    @Autowired
    User user;
    @Override
    public void save() {
        System.out.println(user.toString());
    }
}

xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--注解扫描-->
    <!--bean对象的属性介绍:
                     scope:对象的类型 创建单对象 singleton(文件加载时创建)   创建多对象 prototype(需要时创建)
                      init-method:初始化   destroy-method:销毁
    -->
    <context:component-scan base-package="com.adtchengdu" ></context:component-scan>

     <!--配置数据源-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!--jdbctempl对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

sping-Junit整合:实际开发使用的较多

package com.atchengdu;

import com.adtchengdu.service.Usersive;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class springJunittest {
    @Autowired
    private Usersive usersive;

    @Test
    public  void  test(){
//        usersive.save();
        System.out.println(usersive.getonestudnet(1));
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值