Spring学习Ⅲ

Spring 使用注解代替xml

jar包

jar

工程目录

在这里插入图片描述

配置文件

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: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="test"></context:component-scan>
</beans>

代码

package test;

public interface UserService {

    public void addUser();

}

package test;

import org.springframework.stereotype.Component;

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

测试文件

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

public class Execute {
    @Test
    public void demo02(){
        //从spring容器获得
        String xmlPath = "resource/applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        UserService userService = (UserService) applicationContext.getBean("userServiceId");
        userService.addUser();

    }
}

结果展示

在这里插入图片描述

Spring 依赖注入

普通值注入

配置文件

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: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="test"></context:component-scan>
</beans>

代码

package test;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("user")
public class User {
    @Value("xieyaoyan")
    private String name;


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

测试文件

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.StudentAction;
import test.User;

public class Execute {
    @Test
    public void demo02(){
        //从spring容器获得
        String xmlPath = "resource/applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        User studentAction = (User) applicationContext.getBean("user");
        System.out.println("studentAction = " + studentAction);
    }
}

结果展示

在这里插入图片描述

对象注入

配置文件

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: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="test"></context:component-scan>
</beans>

代码

package test;

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();
    }

}

package test;

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

import javax.annotation.Resource;


@Service
public class StudentServiceImpl implements StudentService {

    private StudentDao studentDao;

    /**
     * @Autowired
     * @Qualifier("studentDaoId")
     * 上面两行为一种方式
     * 下面一行为另一种方式
     * @Resource(name = "studentDaoId")
     * @param studentDao
     */
    @Autowired
    @Qualifier("studentDaoId")
//    @Resource(name = "studentDaoId")
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

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

}

package test;

public interface StudentDao {

    void save();

}

package test;

import org.springframework.stereotype.Repository;

@Repository("studentDaoId")
public class StudentDaoImpl implements StudentDao {

    @Override
    public void save() {
        System.out.println("dao");
    }

}

package test;

public interface StudentService {

    void addStudent();

}

测试代码

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

public class Execute {
    @Test
    public void demo02(){
        //从spring容器获得
        String xmlPath = "resource/applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        StudentAction studentAction = (StudentAction) applicationContext.getBean("studentActionId");
        studentAction.execute();
    }
}

结果展示

在这里插入图片描述
初始化:@PostConstruct
销毁:@PreDestroy
@Scope(“prototype”) 多例

public interface UserService {
	
	public void addUser();

}

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

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

@Service("userServiceId")
//@Scope("prototype")
public class UserServiceImpl implements UserService {

	@Override
	public void addUser() {
		System.out.println("d_scope add user");
	}
	
	@PostConstruct
	public void myInit(){
		System.out.println("初始化");
	}
	@PreDestroy
	public void myDestroy(){
		System.out.println("销毁");
	}

}

注意

<context:annotation-config>该标签,只在xml和注解(注入)混合使用时,使注入注解生效。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值