spring学习4之注解配置

第一步:在maven中写jar包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.pp</groupId>
    <artifactId>spring_day2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.5.RELEASE</version>
        </dependency>
    </dependencies>


    <!--为了让src下的包都编译进去-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

第二步:写applicationContext.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:p="http://www.springframework.org/schema/p"
       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
">
</beans>

第三步:在根目录创建com.pp.demo1包

  1. 在demo1下新建一个UserDao接口类
package com.pp.demo1;

public interface UserDao {
    public void save();
}

  1. 在demo1下创建一个UserDaoImpl继承UserDao
package com.pp.demo1;

import org.springframework.stereotype.Repository;

public class UserDaoImpl implements UserDao{

    public void save() {
        System.out.println("保存数据");
    }
}

  1. 在demo1下创建UserService
package com.pp.demo1;

public interface UserService {
    public void sayhello();
}

  1. 在demo1下创建UserServiceImpl实现UserService
package com.pp.demo1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

public class UserServiceImpl implements UserService {
   
    public void sayhello() {
        System.out.println("hello");
    }
}

第四步:配置文件中设置注解扫描

    <!--注解扫描-->
<context:component-scan base-package="com.pp"/>

第五步:实现类上进行注解

注解分为三层架构,一般把Dao相关的实现类上面加上注解@Repository(value = “userDao”),Repository中的value通长写上实体类的小写。在service的实现类上加入注解@Component(“abc”)等效于上一个章节配置文件中的

第六步:创建DemoSpring测试类

package com.pp.demo1;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoSpring {
    @Test
    public void fun(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService abc = (UserService) context.getBean("abc");
        abc.sayhello();
    }
}

第七步:测试
在这里插入图片描述

扩展

  • 扩展一之属性注解

按照id进行注入,即在service层调用dao层的注解@Autowired

首先,在service的实现层上进行修改

package com.pp.demo1;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Component("abc")  //Component常用于service层的注解
public class UserServiceImpl implements UserService {
    @Autowired
    public UserDao userDao;

    public void sayhello() {
        System.out.println("hello");
        userDao.save();
    }
}

代码解析:以上代码中,service层加入了dao层的类,在调用的时候,由于该类是个需要创建,因此也需要去注解管理,也就是依赖注入。所以需要在dao类中进行 @Autowired注解。
然后,再次调用测试类的方法:

    @Test
    public void fun(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService abc = (UserService) context.getBean("abc");
        abc.sayhello();
    }

测试结果:
在这里插入图片描述
另外dao的注解还可以使用Resource通常要又value,而value的值默认写定义的变量名

@Component("abc")  //Component常用于service层的注解
public class UserServiceImpl implements UserService {
    @Resource(name="userDao")  //相当于@Autowired和@Qualifier一起使用,该注解等效于以下两个注解一起使用
    /*@Autowired
    @Qualifier(value = "userDao")*/
    public UserDao userDao;

    public void sayhello() {
        System.out.println("hello");
        userDao.save();
    }
}
  • 扩展二之业务层的注解
    由于UserServiceImpl属于service层所以这里的可以使用@Service注解,但是工厂里是通过 id来拿注解的,所以可以使用@Service(value = “abc”) 使用的相关的代码如下:
/*@Component("abc")  //Component常用于service层的注解*/
@Service(value = "abc")  //既然是业务层就可以将@Component("abc")替换为@Service(value = "abc") 达到效果是一样的
public class UserServiceImpl implements UserService {
   // @Autowired  //该注解常用于调用dao层的注解Repository  叫做按照id进行依赖注入
    @Resource(name="userDao")  //相当于@Autowired和@Qualifier一起使用,该注解等效于以下两个注解一起使用
    /*@Autowired
    @Qualifier(value = "userDao")*/
    public UserDao userDao;

    public void sayhello() {
        System.out.println("hello");
        userDao.save();
    }
}

源码地址:https://gitee.com/yangforever/project-learning/tree/master/demo/Spring/springday2

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值