@Autowired用法详解
@Autowired注解详解——超详细易懂
@Autowired的用法和作用
Autowired must be defined in Spring bean @Component/@Service
导致@Autowired注入失败的问题
Spring@Autowired注解与省去get和set方法,对注解Autowired放在setter方法上的情况
Java开发技术之 SSM框架的学习分享
Spring注解之@Autowired失败的解决方案
springboot中@Autowired 注入失效的四种原因及解决方法
Spring@Autowired注解与省去get和set方法,对注解Autowired放在setter方法上的情况
@Autowired详解
要搞明白@Autowired注解就是要了解它是什么?有什么作用?怎么用?为什么?
首先了解一下IOC操作Bean管理,**bean管理是指(1)spring创建对象 (2)spring注入属性。**当我们在将一个类上标注@Service或者@Controller或@Component或@Repository注解之后,spring的组件扫描就会自动发现它,并且会将其初始化为spring应用上下文中的bean。 当需要使用这个bean的时候,例如加上@Autowired注解的时候,这个bean就会被创建。而且初始化是根据无参构造函数。
先看代码来体会一下这个注解的作用,测试代码如下:(@Data注解是由Lombok库提供的,会生成getter、setter以及equals()、hashCode()、toString()等方法)
@Autowired 这个注解就是spring可以自动帮你把bean里面引用的对象的setter/getter方法省略,它会自动帮你set/get。
<bean id="userDao" class="..."/>
<bean id="userService" class="...">
<property name="userDao">
<ref bean="userDao"/>
</property>
</bean>
这样你在userService里面要做一个userDao的setter/getter方法。
但如果你用了@Autowired的话,你只需要在UserService的实现类中声明即可。
@Autowired
private IUserDao userdao;
在applicatonContext.xml中 把原来 引用的标签也去掉。
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor 将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。
按照上面的配置,Spring 将直接采用 Java 反射机制对 Boss 中的 car 和 office 这两个私有成员变量进行自动注入。所以对成员变量使用 @Autowired 后,您大可将它们的 setter 方法(setCar() 和 setOffice())从Boss 中删除。
public class Boss {
@Autowired
private Car car;
@Autowired
private Office office;
}
<bean id="userDao4" class="cn.itcast.spring.UserDaoImpl" />
<bean id ="userService" class="cn.itcast.spring.UserServiceImpl">
<!-- 注入对象 -->
<!-- property 根据类中的setter方法进行属性注入 -->
<!-- name:setter方法的后缀小写,比如setXxx 对应的name为xxx -->
<!-- ref:引用哪一个bean(对象),值为bean的id/name -->
<!--用了Autowired就删掉Property,ref,否则报错-->
<!-- <property name="userDao4" ref="userDao4" />-->
</bean>
App.java
import beans.User;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
User user1 = (User) context.getBean("user");
user1.sayHi("Spring1");
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
User user2 = beanFactory.getBean(User.class);
user2.sayHi("Spring2");
User user3 = context.getBean("user",User.class);
user3.sayHi("Spring3");
}
}
IUserDao
package cn.itcast.spring;
public interface IUserDao {
public void findByUsernameAndPassword();
}
IUserService
package cn.itcast.spring;
public interface IUserService {
public void login();
}
UserDaoFactory
package cn.itcast.spring;
public class UserDaoFactory {
public UserDaoImpl getUserDAO() {
return new UserDaoImpl();
}
public Object getBean() {
Object bean = null;
try {
bean = Class.forName("cn.itcast.spring.UserDaoImpl").newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return bean;
}
}
UserDaoImpl
package cn.itcast.spring;
public class UserDaoImpl implements IUserDao {
public static int count = 1;
public static void countMethod() {
count++;
}
@Override
public void findByUsernameAndPassword() {
System.out.println("UserDaoImpl-dao层被调用" + count + "次了");
countMethod();
}
}
UserServiceImpl
package cn.itcast.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao userDao4;
@Override
public void login() {
System.out.println("UserServiceImpl-service层被调用了。。。");
IUserDao userDao = new UserDaoImpl();
userDao.findByUsernameAndPassword();
UserDaoFactory userDaoFactory = new UserDaoFactory();
UserDaoImpl userDao1 = userDaoFactory.getUserDAO();
userDao1.findByUsernameAndPassword();
IUserDao userDao2 = (IUserDao) userDaoFactory.getBean();
userDao2.findByUsernameAndPassword();
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserDao userDao3 = (IUserDao) ac.getBean("userDao3");
userDao3.findByUsernameAndPassword();
userDao4.findByUsernameAndPassword();
}
}
UserServiceImplTest
package cn.itcast.spring;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserServiceImplTest {
@Test
public void login() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserService userService1 = (IUserService) ac.getBean("userService");
userService1.login();
}
}
log4j.properties
log4j.rootLogger=INFO,A1
log4j.logger.org.apache=INFO
log4j.appender.A1.Target=System.err
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
spring-config.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<beans>
<bean id = "user" class="beans.User"></bean>
</beans>
</beans>
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="userDao3" class="cn.itcast.spring.UserDaoImpl" />
<bean id="userDao4" class="cn.itcast.spring.UserDaoImpl" />
<bean id ="userService" class="cn.itcast.spring.UserServiceImpl">
</bean>
</beans>
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>org.example</groupId>
<artifactId>spring1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
|