@File
2019-08-19T08:13:57.000000Z
字数 3138
阅读 174
java
<?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"><!-- id/name: 调用是的名称 --><!-- class: 类的位置 --><bean id="ldy" class="com.lidaye.Lidaye"></bean><bean id="uif" class="com.lidaye.UserInfo"></bean></beans>
<?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 id="ldy" class="com.lidaye.Lidaye"><!-- constructor-age: 通过构造方法设置属性值 --><!-- 通过构造函数参数位数赋值 --><constructor-arg index="0" type="int" value="1"/><!-- 通过参数名称赋值 --><constructor-arg name="name" value="lidaye"/><!-- 通过参数名赋予list --><constructor-arg name="dream"><list><value>soldier</value><value>scientist</value><value>pilot</value></list></constructor-arg><!-- 通过参数名赋予map --><constructor-arg name="score"><map><entry key="math" value="90"/><entry key="english" value="85"/></map></constructor-arg><!-- 通过参数名赋予另一个对象 --><constructor-arg name="info" ref="uif"/></bean><bean id="uif" class="com.lidaye.UserInfo"></bean></beans>
<?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 id="ldy" class="com.lidaye.Lidaye"><!-- property: set方法修改属性值 --><!-- 通过属性名赋予值 --><property name="name" value="lidaye" /><!-- 通过属性名赋予对象 --><property name="info" ref="uif" /></bean><bean id="uif" class="com.lidaye.UserInfo"></bean></beans>
<!-- beans 属性要引入 xmlns:context --><?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:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://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.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 配置spring注解扫描位置 --><context:component-scan base-package="com.lidaye" /><!-- 添加spring-mvc注解支持 --><mvc:annotation-driven /></beans>
@Component 注册类(通用)
// 在实现类上通过 Component 注册@Component() // 无参时,调用名为类名:Lidaye@Component("ldy") // 调用名为ldypublic class Lidaye{}
@Service 注册(建议业务层用,效果一样)@Controller 注册(建议控制层用,效果一样)@Repository 注册(建议数据层用,效果一样)@Autowired spring自带注入(非线程安全)
// 根据属性或参数的类型找// 不强制对象存在,默认强制@Autowired(required = false)
@Resource jsr250注入(线程安全)
// 默认根据属性名或参数名找,找不到再按照类型找// 设置注册的名@Resource(name = "注册的名称")