Spring学习第二天------2021-07-14-注解导入

Spring学习第二天------2021-07-14-注解导入

注解

1.配置pom.xml

<?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.zx</groupId>
    <artifactId>20210717_Spring03_zhujie</artifactId>
    <version>1.0-SNAPSHOT</version>


    <!--Spring IOC-->
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>
</project>

2.配置beans.xml

	<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bean="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
    <!--开启注解-->
    <context:annotation-config/>
    <!--开启自动扫描-->
    <context:component-scan base-package="com.zx"></context:component-scan>
</beans>

3.其它

1、在需要被找到的类上方添加@Component

也就是在需要做控制反转的类上标明注解@Component

@Component
public class RoleDaoImpl implements RoleDao {
    @Override
    public void addRole(Role role) {
        System.out.println("中间表添加角色");
    }
}

或者

若在Servlet层,可将@Component注解改为@Controller

@Controller()
public class UserServlet {
    @Resource()
    private UserService userService ;
    public void add() {
        User user = new User();
        Role role = new Role();
        userService.add(user,role);
    }

}

若在Service层,可将@Component注解改为@Service

@Service
public class UserServiceImpl implements UserService {
    //@Autowired
    @Resource
    private UserDao userDao ;
    //@Autowired
    @Resource
    private RoleDao roleDao ;

    @Override
    public void add(User user, Role role) {
        System.out.println("开启事务");
        userDao.add(user);
        roleDao.addRole(role);
        System.out.println("提交事务");
    }
}

若在Dao层,可将@Component注解改为@Repository

@Repository

public class RoleDaoImpl implements RoleDao {
    @Override
    public void addRole(Role role) {
        System.out.println("中间表添加角色");
    }
}

2、在需要找的属性上方添加@Autowired

也就是在需要的私有属性上方标明@Autowired注解

 @Autowired
    private UserService userService ;

或者
写成@Resource属性

@Resource()
private UserService userService ;

若是想给普通属性赋值可写@Value注解

 @Value("15")
    private int age;

【notes】

@Autowired或者@Resource默认是ByType来注入,需要提供唯一的一个类型能匹配的id标签,但是如果有好几个类型都能匹配,那么在注解中可以通过,名称来指定

或者@Resource(name=’’‘xxx’),xxx为另一个类型的类名首字母小写,也就是说在注解里写一个字符串就相当于它的新名字,别人就必须通过这个新名字来找到它,旧名字就失效了

这些注解本身都是通过byType来寻找的,如果说它们的 类型都一致,就需要根据名字来寻找了,这个时候就需要在被调用的类的注解上添加一个名字,然后再调用它的属性的注解上加上name=“xxx”即可

【Note】 【总结】字节的类采用注解方式,第三方采用xml方式

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值