wordpress开通多站点好处,介绍家乡的网站怎么做,rpc wordpress,鬼佬做爰网站一、引子
我们在前面向读者介绍了Spring的配置文件#xff0c;可以在配置文件中声明Bean#xff0c;把对象的创建权交给Spring容器#xff0c;并且实际演示了如何配置一个经典的Bean——数据源的配置。经过实际的上手我们会发现#xff1a;xml的配置显得有些繁重#xff…一、引子
我们在前面向读者介绍了Spring的配置文件可以在配置文件中声明Bean把对象的创建权交给Spring容器并且实际演示了如何配置一个经典的Bean——数据源的配置。经过实际的上手我们会发现xml的配置显得有些繁重于是更便捷的配置方式便应运而生了——注解开发。
二、快速使用
1在类上添加注解
Spring框架有4个原生注解用于声明需要放至于IoC容器中分别是ControllerServiceRepositoryComponent。他们没有实际的区别仅在于后续划分代码结构时各个分层上一般使用不同的注解。
import com.bylearning.spring.dao.UserDao;
import org.springframework.stereotype.Repository;Repository
public class UserDaoImpl implements UserDao {Overridepublic void saveUser() {System.out.println(save successfully);}
}2如何DI
Spring框架也提供了3个原生注解AutowiredResourceQualifier。Autowired是用于按照类型byType装配依赖对象。默认情况下它要求依赖对象必须存在如果允许null值可以设置它的required属性为false。如果想要使用按照名称byName来装配可以结合Qualifier注解一起使用。Resource注解默认按照名称进行装配。名称可以通过name属性进行指定如果没有指定name属性当注解写在字段上时默认取字段名进行安装名称查找如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。需要注意的是如果name属性一旦指定就只会按照名称进行装配。Qualifier注解用于解决歧义性问题当有多个相同类型的Bean时可以使用该注解指定要注入的Bean。
总结来说Autowired主要用于按照类型装配依赖对象可以结合Qualifier注解使用Resource默认按照名称装配依赖对象可以指定name属性进行配置Qualifier用于解决歧义性问题指定要注入的Bean。
import com.bylearning.spring.dao.UserDao;
import com.bylearning.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;Service
public class UserServiceImpl implements UserService {AutowiredQualifier(value userDaoImpl)private UserDao userDao;Overridepublic void saveUser() {userDao.saveUser();}
}3配置包扫描路径
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsdcontext:component-scan base-packagecom.bylearning.spring /
/beans
三、思考 我们拥有了上面的3个注解后似乎可以将大部分的xml配置转化为注解了但是请读者思考有哪些情景依靠这3个注解无法注解化呢下面给出了场景与对应的解决办法
xml配置文件还保留着 -- Configuration我们在引子中提到的数据源Bean注册 -- Bean组件扫描 -- ComponentScan数据源中引用的properties文件 -- PropertySource导入的其它配置类 -- Import
在以下例子中主配置文件借助Configuration注解将类声明为配置类并用ComponentScan注解对包进行扫描将该包下的类含有Component等声明为Spring组件的注解添加进IoC容器借助Import注解引入了另一个配置文件——数据源配置文件在数据源配置文件中利用PropertySource注解引入了一个Properties文件利用Bean注解注册了一个Druid数据源。
主配置文件
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;Configuration
ComponentScan(com.bylearning.spring)
Import({DataSourceConfiguration.class})
public class SpringConfiguration {}数据源配置文件
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;import javax.sql.DataSource;PropertySource(classpath:jdbc.properties)
public class DataSourceConfiguration {Value(${jdbc.driver})private String driver;Value(${jdbc.url})private String url;Value(${jdbc.username})private String username;Value(${jdbc.password})private String password;Beanpublic DataSource getDataSource() {DruidDataSource dataSource new DruidDataSource();dataSource.setDriverClassName(driver);dataSource.setUrl(url);dataSource.setUsername(username);dataSource.setPassword(password);return dataSource;}}掌握了这些注解后可以大大减轻xml配置的繁重工作了。我们可以再回顾一下在引子中提到的Spring的配置开发附链接对比一下将更能发现注解开发的便捷性。