装配Bean基于注解

注解

就是一个类 使用方式 @使用名称

使用注解取代xml配置文件:

1 @component    取代<bean class = " "> 

 @componet("id")  取代<bean id = "id"   class = " " >

注意:web开发中提供3个@component注解衍生注解(功能一样) 取代<bean>

@repository: dao层

@service:service层

@controller: web层



2依赖注入:给私有字段设置,也可以给setter方法设置

普通值 : @value("")

引用值: 方式3:

                           1:按照[类型]注入 默认 @Autowired

                           2:按照[名称]1注入     @Autowired @Qualifier("名称")

                           3:按照[名称]2注入     @resource("名称")

3 生命周期  初始化@PostConsturct  销毁@PreDestroy

4作用域 @Scope("prototype") eg多例

案例1

目标类

package com.itheima.a_ioc;

import org.springframework.stereotype.Component;

@Component("userServiceId")
public class UserServiceImpl implements UserService {

	@Override
	public void addUser() {
		System.out.println("annotation add user");
	}
}


配置

<?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">

<context:component-scan base-package="com.itheima.a_ioc"></context:component-scan>
	
</beans>


测试

package com.itheima.a_ioc;

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

public class TestIoC {
	
	@Test
	public void demo02(){

		String xmlPath = "com/itheima/a_ioc/beans.xml";
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		UserService userService = (UserService) applicationContext.getBean("userServiceId");
		userService.addUser();		
	}
}


案列2

目标类

package com.itheima.g_annotation.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() ;
	}
}

package com.itheima.g_annotation.web;

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


package com.itheima.g_annotation.web;

import org.springframework.stereotype.Repository;

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

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

}


2配置

<?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">

<context:component-scan base-package="com.itheima.g_annotation.web"></context:component-scan>
	
</beans>

测试

package com.itheima.g_annotation.web;

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

public class TestWeb {
	@Test
	public void demo01(){
		String xmlPath = "com/itheima/g_annotation/web/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath) ;
		StudentAction studentAction = applicationContext.getBean("studentActionId", StudentAction.class) ;
		studentAction.execute() ;
	}

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值