【Spring框架】Spring入门(五)——注解

注解:就是一个类,使用@注解名称,使用注解可以取代 xml配置文件。

案例一:

1.使用@Component取代<bean class="">@Component("id") 取代 <bean id="" class="">

UserServiceImpl实现类:

import org.springframework.stereotype.Component;

@Component("UserServiceId")
public class UserServiceImpl implements UserService {
    @Override
    public void addUser() {
        System.out.println("g_annotation.a_ioc_user");
    }
}

bean.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="UserServiceId" class="com.spring.g_annotation.a_ioc.UserServiceImpl"></bean>
        -->
        <!--注解扫描,扫描含有注解的类-->
        <context:component-scan base-package="com.spring.g_annotation.a_ioc"></context:component-scan>
</beans>

测试类:

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

public class TestAnnoIoc {
    @Test
    public void demo02() {
        //从spring容器获得
        //1.获取容器
        String xmlPath = "com/spring/g_annotation/a_ioc/beans.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        //2.获得内容,不需要自己new一个对象
        UserService userService = (UserService) applicationContext.getBean("UserServiceId");
        userService.addUser();
    }
}

//运行结果:g_annotation.a_ioc_user

案例二:

2.在web开发,提供3@Component注解衍生注解(功能一样)取代<bean class="">
	@Repository :dao层
	@Service:service层
	@Controller:web层
3.依赖注入,给私有字段设置,也可以给setter方法设置
	普通值:@Value("")
	引用值:
		方式1:按照【类型】注入
			@Autowired
		方式2:按照【名称】注入1
			@Autowired
			@Qualifier("名称")
		方式3:按照【名称】注入2
			@Resource("名称")

Dao层:

import org.springframework.stereotype.Repository;

@Repository("studentDaoId")
public class StudentDaoImpl implements StudentDao{
    @Override
    public void save() {
        System.out.println("Dao");
    }
}

Service层:

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

@Service
public class StudentServiceImpl implements StudentService{
    private StudentDao studentDao;
    @Autowired
    @Qualifier("studentDaoId")
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    @Override
    public void addStudent() {
        studentDao.save();
    }
}

Web层:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller("StudentActionId")
public class StudentAction {
    @Autowired//默认按照类型
    private StudentService studentService;

    public void execute() {
        studentService.addStudent();
    }
}

bean.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">
        <!--注解扫描,扫描含有注解的类-->
        <context:component-scan base-package="com.spring.g_annotation.b_web"></context:component-scan>
</beans>

测试类:

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

public class TestAnnoIoc {
    @Test
    public void demo02() {
        //从spring容器获得
        //1.获取容器
        String xmlPath = "com/spring/g_annotation/b_web/beans.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        //2.获得内容,不需要自己new一个对象
        StudentAction studentAction = (StudentAction) applicationContext.getBean("StudentActionId");
        studentAction.execute();
    }
}

//运行结果:Dao

案例三:

4.生命周期
	初始化:@PostConstruct
	销毁:@PreDestroy
5.作用域
	@Scope("prototype") 多例

UserServiceImpl实现类:

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

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

@Service("UserServiceId")
//@Scope("prototype")//多例
public class UserServiceImpl implements UserService {
    @Override
    public void addUser() {
        System.out.println("g_annotation.c_other");
    }

    @PostConstruct
    public void myInit(){
        System.out.println("初始化");
    }

    @PreDestroy
    public void myDestory(){
        System.out.println("销毁");
    }
}

bean.xml:(注解的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">
        <!--注解扫描,扫描含有注解的类-->
        <context:component-scan base-package="com.spring.g_annotation.c_other"></context:component-scan>
        <!--<context:annotation-config></context:annotation-config>-->
</beans>

测试类:

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

public class TestOther {
    @Test
    public void demo02() {
        //spring工厂
        String xmlPath = "com/spring/g_annotation/c_other/beans.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        UserService userService1 = applicationContext.getBean("UserServiceId", UserService.class);
        //UserService userService2 = applicationContext.getBean("UserServiceId", UserService.class);
        System.out.println(userService1);
        //System.out.println(userService2);

        ((ClassPathXmlApplicationContext) applicationContext).close();
    }
}

//运行结果:
/*
初始化
com.spring.g_annotation.c_other.UserServiceImpl@70be0a2b
销毁
*/

*注解使用的前提:添加命名空间,让spring扫描含有注解类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

谦风(Java)

一起学习,一起进步(✪ω✪)

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

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

打赏作者

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

抵扣说明:

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

余额充值