Spring基础知识学习总结(三)

a. xml 方式配置实现案例

1> userDao

public interface UserDao {
    public void save();
}

2>  userDaoImpl

public class UserDaoImpl implements UserDao {
    @Override
    public void save() {
        System.out.println("save running...");
    }
}

3> userService

public interface UserService {
    public void save();
}

4> userServiceImpl

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void save() {
        userDao.save();
    }
}

 5> userServiceImpl

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void save() {
        userDao.save();
    }
}

 6> applicationContext.xml

<bean id="userDao" class="com.xdr630.dao.impl.UserDaoImpl"></bean>

<bean id="userService" class="com.xdr630.service.impl.UserServiceImpl">
	<property name="userDao" ref="userDao"/>
</bean>

7> UserController

public class UserController {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = app.getBean(UserService.class);
        userService.save();
    }
}

8> 测试结果

 

 


 

b. spring基于注释方式装配Bean

 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率。

Spring从2.0开始引入基于注解的配置方式,并且不断的进行完善。通过注解的方式可以直接在类上定义Bean的信息,非常方便。

(1)什么是注解


- 注解是代码特殊标记,格式:@注解名称(属性名称=属性值, 属性名称=属性值…)
- 使用注解,注解作用在类上面,方法上面,属性上面
- 使用注解目的:简化 xml 配置


(2)Spring注解的配置xml文件

使用注解进行开发时,需要在applicationContext.xml中配置组件扫描,作用是指定哪个包及其子包下的Bean需要进行扫描以便识别使用注解配置的类、字段和方法

这里我起名为note.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
">
    <!--
    使用注解之前,我们要先导入4+2+aop的jar包
    同时引入约束 beans+context
    -->

    <!--组件扫描:Spring容器会扫描这个包里所有类,从类的注解信息中获取Bean的信息-->
    <context:component-scan base-package="com.hhh.entity"></context:component-scan>
</beans>

(3)Spring原始常见注解

@Component //使用在类上用于实例化Bean(可以在该注解写上该bean id的值)@Component("")
@Controller //使用在web层类上用于实例化Bean
@Service //使用在service层类上用于实例化Bean
@Repository //使用在dao层类上用于实例化Bean
//上面四个都是直接把类实例化到Spring容器中四个都一样的有用只是分管的区域不一样,
//为了好区分该实例化层是具体的那一次就造就了这三个注解@Controller ,@Service,@Repository。

@Autowired //使用在字段上用于根据类型依赖注入(前提是该字段在spring容器中存在实例化的bean)
//如果Spring容器中有多个的同类型的实例化bean  例如 有俩个同类型的bean. id=user1, id=user2
//就必须在该字段上+@Autowired @Qualifier("user1")指定id
@Qualifier //结合@Autowired一起使用用于根据名称(bean的id)进行依赖注入

@Resource //相当于@Autowired+@Qualifier,按照名称(bean的id)进行注入
//可在字段上注入值 也可以注入spring 加载后端properties文件的值
//例如@value("${properties文件里的键名称}")
@Value //注入普通属性

@Scope //标注Bean的作用范围(之前的 singleton,prototype属性)

@PostConstruct //使用在方法上标注该方法是Bean的初始化方法
@PreDestroy //使用在方法上标注该方法是Bean的销毁化方法

 

 

(4)xml配置文件+原始注解开发测试 

1> AccountDao
package com.hhh.Dao;

public interface AccountDao {
    public void save();
}
2> AccountDaoImpl
package com.hhh.Dao.Impl;

import com.hhh.Dao.AccountDao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

//相当于在xml中配置了
//<bean id="accountDao" class="com.hhh.Dao.Impl.AccountDaoImpl"></bean>
//@Component("accountDao")
@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {

    @Override
    public void save() {
        System.out.println("save running...");
    }
}
3> AccountService
package com.hhh.Service;

public interface AccountService {
    public void save();
}
4> AccountServiceImpl

 

package com.hhh.Service.Impl;

import com.hhh.Dao.AccountDao;
import com.hhh.Service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

//相当于在xml中配置了
//<bean id="accountService" class="com.hhh.Service.Impl.AccountServiceImpl">
//@Component("accountService")
@Service("accountService")
@Scope("singleton")//单例模式
//@Scope("prototype")//多例模式
public class AccountServiceImpl implements AccountService {
/**
    //相当于在xml中配置了
    //<bean id="accountService" class="com.hhh.Service.Impl.AccountServiceImpl">
    //        <property name="accountDao" ref="accountDao"/>
    //</bean>
    //  @Autowired //按照类型注入
    //  @Qualifier("accountDao")//按照名称注入,要和@Autowired一起使用
    //  @Resources 相当于@Autowired 和 @Qualifier
 */
    @Resource(name = "accountDao")
    private AccountDao accountDao;

/**
 *  用@Value 注入普通数据类型,如:把 张三 注入给 name
 *  使用@Value进行字符串的注入
 */

//    @Value("张三")
    @Value("${jdbc.driver}")
    private String name;

//    public void setAccountDao(AccountDao accountDao) {
//        this.accountDao = accountDao;
//    }
    /**
     * 使用注解方式时,可以省略 set 方法。如:把 AccountServiceImpl 中的 set 方法删掉,也能成功运行:
     * 因为把注解放在属性上,直接通过反射为属性赋值,下面的方法也可以使用被赋值的属性了
     * 但如果使用 xml 配置的时候 set 方法是不能省略的
     * 把上面的 @Qualifier("accountDao") 注释掉,也能成功运行,直接写@Autowired也能注入。因为@Autowired 是按照数据类型从Spring容器中进行匹配的,当Spring扫描到这个注解之后,它会直接从Spring容器当中找一个 userDao 类型的 bean,找到之后直接注入。如果容器中 UserDao 类型有多个bean,就不能这样写了 。
     * 按照类型注入,@Autowired
     * 按照名称注入,@Autowired 和 @Qualifier 要一起使用
     */

    @Override
    public void save() {
        System.out.println("service running..." + name);
        accountDao.save();
    }
//使用@PostConstruct标注初始化方法,使用@PreDestroy标注销毁方法
    @PostConstruct
    public void init(){
        System.out.println("对象初始化");
    }

    @PreDestroy
    public void destroy(){
        System.out.println("对象销毁");
    }
}
5>  note.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 id="accountDao" class="com.hhh.Dao.Impl.AccountDaoImpl"></bean>-->

<!--    <bean id="accountService" class="com.hhh.Service.Impl.AccountServiceImpl">-->
<!--        <property name="accountDao" ref="accountDao"/>-->
<!--    </bean>-->

<!--    组件扫描:Spring容器会扫描这个包里所有类,从类的注解信息中获取Bean的信息-->
    <context:component-scan base-package="com.hhh"></context:component-scan>

<!--    加载外部的properties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="root"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
</beans>
6> AccountController

 

package com.hhh.Controller;

import com.hhh.Service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AccountController {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("note.xml");
        AccountService accountService = app.getBean(AccountService.class);
        accountService.save();
        app.close();
    }
}
7> 测试结果

 

  • 23
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚睡早起₍˄·͈༝·͈˄*₎◞ ̑̑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值