Spring增对Bean管理中创建对象提供注解 @Component @Service @Controller @Repository
利用注解实现对象创建 1.引入依赖jar包 2.开启组件扫描 3.创建类,在类上面加上注解
简单样例
fym.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">
<!--开启组件扫描
1.如果扫描多个包,多个包使用逗号隔开
2.扫描包上层目录-->
<context:component-scan base-package="com.fym"></context:component-scan>
</beans>
package com.fym.service;
import org.springframework.stereotype.Component;
/*
在注解里面value属性可以省略不写,默认值是类名称,首字母小写
使用4大注解中的任何一个注解都可以,但是一般有使用习惯
*/
@Component(value = "userService")//等价于注解注入bean
public class UserService {
public void add(){
System.out.println("service add...");
}
}
package com.fym;
import com.fym.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
利用注解实现对象创建
1.引入依赖jar包
2.开启组件扫描
3.创建类,在类上面加上注解
*/
public class SpringIocTest {
@Test
public void testService(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("fym.xml");
UserService userService = applicationContext.getBean("userService", UserService.class);
userService.add();
System.out.println(userService);
}
}
测试结果
开启组件扫描中细节的配置
<context:component-scan base-package="com.fym" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
</context:component-scan>
使用该配置,只扫描有Compoment注解的类
<context:component-scan base-package="com.fym" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component"/>
</context:component-scan>
与上面相反
——————————————————————————————————————————————————
基于注解实现属性注入 @AutoWired:根据属性类型进行自动装配 @Qualifier:根据属性名称进行注入 @Resource:可以根据类型注入,也可以根据名称注入 @Value:注入普通类型属性
package com.fym.service;
import com.fym.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
/*
在注解里面value属性可以省略不写,默认值是类名称,首字母小写
使用4大注解中的任何一个注解都可以,但是一般有使用习惯
*/
@Service(value = "userService")//等价于注解注入bean
public class UserService {
//定义dao类型属性
//不需要set方法
@Autowired
@Qualifier(value = "userDaoImpl1")//一般与Autowired搭配使用,根据名称注入
private UserDao userDao;//根据类型注入
@Value(value="abc")//普通属性注入
private String str;
public void add(){
userDao.add();
System.out.println("service add..." + str);
}
}
package com.fym.dao;
public interface UserDao {
public void add();
}
package com.fym.dao;
import org.springframework.stereotype.Repository;
@Repository(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao{
@Override
public void add() {
System.out.println("userDaoImpl add...");
}
}
___________________________________________________________________________________
完全注解开发(一般不用,用springboot代替)
用该class文件代替xml文件
package com.fym.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration//作为配置类,替代xml文件
@ComponentScan(basePackages = {"com.fym"})
public class SpringConfig {
}
@Test
public void testService1(){
//加载配置类
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = applicationContext.getBean("userService", UserService.class);
userService.add();
System.out.println(userService);
}