IOC操作Bean管理
外部属性文件
在我们编写配置文件的时候,经常要去在Bean注入一些对接数据库、nacos等一些系统部署信息的细节信息,如果直接把这些东西写到配置文件里面操作起来非常的不方便,为了优化这种问题,就出现了外部属性文件
如何直接配置数据库信息?
第一步:引入德鲁伊连接池依赖jar包
<?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" >
<!--直接配置连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>
<property name="username" value="root"></property>
<property name="password" value="2022"></property>
</bean>
</beans>
引入外部属性文件配置数据库连接池
第一步:创建外部属性文件,properties格式文件,写数据库信息
prop.driverClass:com.mysql.jdbc.Driver
prop.url:jdbc:mysql://localhost:3306/userDb
prop.userName:root
prop.password:2022
第二步:把外部properties属性文件引入到spring配置文件中
*引入context名称空间
*在spring配置文件使用标签引入外部属性文件
<?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:property-placeholder location="classpath:jdbc.properties"/>
<!--配置连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClass}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.userName}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
</beans>
IOC操作Bean管理(基于注解方式)
什么是注解?
(1)注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值)
(2)使用注解,注解作用在类上面,方法上面,属性上面
(3)使用注解的目的,简化xml配置
利用注解创建对象
可以实现创建对象的注解有:@Component、@Service、@Controller、@Repository 上面四个注解功能是一样的,都可以用来创建bean实例
第一步 引入依赖
第二步 开启组件扫描
<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">
<!--开启组件扫描
1.如果扫描多个包,多个包逗号隔开
2.扫描上层目录-->
<context:component-scan base-package="com.ioc.spring5"></context:component-scan>
</beans>
第三步 创建类,在类上面添加创建对象注解
//注解里面value属性值可以省略不写,
//默认值是类名称,首字母小写
//UserService -- userService
@Component(value = "userService") //<bean id="userService" class=".."/>
public class UserService {
public void add(){
System.out.println("service add......");
}
}
基于注解方式实现属性注入
(1)@AutoWired:根据属性类型进行自动装配
第一步:把service和dao对象创建,在service和dao类添加创建对象注解
第二步:在service注入dao对象,在service类添加dao类属性,在属性上面使用注解
(2)@Qualifer:根据属性名称进行注入
当一个接口有多个实现类,根据类型用不知道用那个,就要派它上场,和@AutoWired一起来使用了
(3)@Resource:可以根据类型注入,可以根据名称注入
(4)@Value:注入普通类型属性
代码示例:
service类:
//注解里面value属性值可以省略不写,
//默认值是类名称,首字母小写
//UserService -- userService
@Component(value = "userService") //<bean id="userService" class=".."/>
public class UserService {
//定义dao类型属性
//不需要添加set方法
//添加注入属性注解
// @Autowired
// @Qualifier(value = "userDaoImpl")
// private UserDao userDao;
// @Resource //根据类型注入
@Resource(name = "userDaoImpl")
private UserDao userDao;
@Value(value = "abc")
private String name;
public void add(){
System.out.println("service add......"+name);
userDao.add();
}
}
dao类:
@Repository
public class UserDaoImpl implements UserDao{
@Override
public void add() {
System.out.println("dao add");
}
}
dao接口:
public interface UserDao {
public void add();
}
完全注解实现
创建配置类,替代xml配置文件
代码示例:
@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.ioc"})
public class SpringConfig {
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/141576.html