大家好,我是一只学弱狗,记录学习的点点滴滴!
优质文章
一张黄图的故事JavaSE练习项目我是菜鸟、我小试牛刀linux指令太多记不住?小白看这篇就够了!
优质专栏
数据库就该这样学爪哇外步篇
Spring
依赖注入
IOC创建对象方式
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<bean id="user" class="com.kuang.pojo.User">
<constructor-arg index="0" value="乐乐1"/>
</bean>
<bean id="user" class="com.kuang.pojo.User">
<constructor-arg name="name" value="乐乐3"/>
</bean>
<alias name="user" alias="uuser"/>
<bean id="manager" class="com.kuang.pojo.Manager" name="manager2,manager3">
<property name="name" value="乐乐乐"/>
</bean>
<import resource="beans.xml"/>
</beans>
常见类型注入
<bean id="student" class="com.kuang.pojo.Student">
<property name="name" value="张三"/>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>红楼梦
</value>
<value>西游记
</value>
<value>水浒传
</value>
<value>三国演义
</value>
</array>
</property>
<property name="hobbys">
<list>
<value>吃饭
</value>
<value>睡觉
</value>
<value>打豆豆
</value>
</list>
</property>
<property name="card">
<map>
<entry key="身份证" value="123456789"/>
<entry key="银行卡" value="987654321"/>
</map>
</property>
<property name="games">
<set>
<value>王者荣耀
</value>
<value>英雄联盟
</value>
</set>
</property>
<property name="info">
<props>
<prop key="学号">123456
</prop>
<prop key="性别">男
</prop>
</props>
</property>
<property name="wife">
<null/>
</property>
</bean>
c命名和p命名注入
<?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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.kuang.pojo.User" p:name="乐乐乐乐" p:age="18"/>
<bean id="user2" class="com.kuang.pojo.User" c:name="可可" c:age="18" scope="prototype"/>
</beans>
Bean的作用域
自动装配
使用byType和byName实现自动装配
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="people" class="com.kuang.pojo.People" autowire="byType">
<property name="name" value="乐乐"/>
</bean>
使用注解实现自动装配
导入约束配置注解支持
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/beans/spring-aop.xsd">
<context:annotation-config/>
</beans>
@Autowired:直接在属性上使用即可,也可以在set方式上使用;首先按照类型查询,其次按照id查询(即set方法后的字段名称),若仍发生冲突,则报错!
@Qualifier(value = “”):可以显示的指定Bean的id值
@Resource:和Autowired类似,先根据类型在ioc容器中查找,如存在多个,则按照set方法后的字段名称去查找,如仍在不存在,则报错,这时,可以指定其name值来装配,故我们可以看作时@Autowired和@Qualifier的结合