1. 默认无参结构
User:其中User为默认无参结构
public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}
beans.xml:(这样不会出错)
<bean id="user" class="com.pojo.User"> <property name="name" value="Bob" /> </bean>
若是User没有了无参结构,只有有参结构,beans.xml 那样机会出错:
public class User { private String name; public User(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
beans.xml:(默认无参结构,这样会出错)
<bean id="user" class="com.pojo.User"> <property name="name" value="Bob" /> </bean>
2. 无参结构怎么实现的值的注入?
答:通过 setter
如没有setter, 也会出错。(能够将 setter正文掉,就会出错)、
3. 有参数结构
若是没有无参结构,只有有参结构,那怎么实现属性值的注入?能够通过:constructor-arg 标签
User:
private String name; public int age; public User(String name, int age) { this.name = name; this.age = age; } // Setter and Getter, toString
1)通过index 注入:
<bean id="user" class="com.pojo.User"> <constructor-arg index = "0" value="Bob" /> <constructor-arg index="1" value="25" /> </bean>
2)通过 type 注入:
<bean id="user" class="com.pojo.User"> <constructor-arg type="java.lang.String" value="Pop"/> <constructor-arg type="int" value="69"/> </bean>
毛病:全局只能存在惟一的类型
3)通过参数名注入:
<bean id="user" class="com.pojo.User"> <constructor-arg name="name" value="Json"/> <constructor-arg name="age" value="54"/> </bean>
4. Spring 其它一些配置
1)alias标签:能够取别名
<alias name="user" alias="userAlias"/>
2)bean中的name属性,能够取多个别名
<bean id="user" class="com.pojo.User" name="userNew1,userNew2,userNew3,..."> <constructor-arg name="name" value="Jack"/></bean>
3)import 标签,多人协同开发:
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="beans.xml"/> <import resource="beans2.xml"/> <import resource="beans3.xml" /></beans>
5. 多种类型的依赖注入
这里次要依赖于无参注入
Student:
public class Address { private String address; //上面是Setter and Getter , toString}public class Student { private String name; private Address address; private String[] texts; private List<String> hobbys; private Map<String, String> card; private Set<String> games; private Properties info; private String graduate; //上面是Setter and Getter , toString}
在Beans.xml 注入
<bean id="address" class="com.pojo.Address" > <property name="address" value="北京"/> </bean> <bean id="student" class="com.pojo.Student" > .... </bean>
对Student各种类型的值进行注入:
一般值类型:name
<property name="name" value="Tom" />
援用类型:address
<property name="address" ref="address" />
数组类型:texts
<property name="texts"> <array> <value> 西游记</value> <value> 活着</value> <value> 游记</value> </array></property>
List类型:hobbys
<property name="hobbys"> <list> <value>上网</value> <value>看书</value> <value>打游戏</value> </list></property>
Map类型:card
<property name="card" > <map> <entry key="学号" value="210012"/> <entry key="舒瑟" value="01201"/> </map></property>
Set类型:games
<property name="games"> <set> <value>LoL</value> <value>王者</value> </set></property>
null类型:graduate
<property name="graduate"> <null /></property>
property类型:info
<property name="info"> <props> <prop key="性别"> 男 </prop> <prop key="国籍"> China </prop> <prop key="username">Nopu</prop> </props> </property>
6. Bean的作用域
通过 Bean外面的 scope属性来确定
scope="singleton | prototype | request | session | application | websocket"
例如:scope = "singleton"
Singleton :单例模式
Prototype 原型模式
Request
Session
Application
WebSocket
7. Bean的主动拆卸
7.1 Bean中autowire属性值实现
在类中,若是有其它的援用对象,Bean能够上下文本人寻找。Spring IoC 容器在上下文中本人寻找Bean, 并进行匹匹配。通过 Bean的autowire属性
- autowire = "byName" or
- autowire = "byType"
代码:
public class Cat { public void shout(){ System.out.println("喵喵喵..."); }}public class Dog { public void shout(){ System.out.println("汪汪汪"); }}public class Zoo { private Cat cat; private Dog dog; //上面是Setter Getter and toString}
其中Zoo有Dog 和Cat 援用值
在Bean,本来应该写为:
<bean id="cat" class="com.pojo.Cat"/> <bean id="dog" class="com.pojo.Dog"/> <bean id="zoo" class="com.pojo.Zoo"> <property name="dog" ref="dog"/> <property name="cat" ref="cat"/> </bean>
咱们通过autowire 主动拆卸后:
<bean id="cat" class="com.pojo.Cat"/> <bean id="dog" class="com.pojo.Dog"/> // 或着autowire = byType <bean id="zoo" class="com.pojo.Zoo" autowire="byName"> </bean>
7.2 @Autowired注解实现
在Zoo类中,援用类型的定义下面增加@Autowired,就能够主动拆卸(赋值)
单须要须要留神的是:须要在 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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 注解的必不可少的配置--> <context:annotation-config/></beans>
public class Zoo { @Autowired private Cat cat; @Autowired private Dog dog; //上面是Setter ,Getter and toString}
在Bean.xml:
<bean id="cat" class="com.pojo.Cat"/> <bean id="dog" class="com.pojo.Dog"/> <bean id="zoo" class="com.pojo.Zoo"/>
留神:1)<bean> 是将类和Bean链接起来,由bean治理,所以定义的类须要在IoC容器里注册(<bean>标签),否则前面操作也没用。其中Zoo有Cat和Dog的援用值,赋值的通过@Autowired来拆卸IoC容易外面有的。
2)@Autowired 默认通过byName来实现的
3) @Autowired(Required=false)则容许该对象为Null
8. 注解开发
8.1. 确保应用注解开发必须保障相应的包导入:
8.2. 导入bens中相应的束缚:
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="user" class="com.pojo.User"/></beans>
8.3 若是要指定范畴下的注解失效:
<context:component-scan base-package="com.pojo"/>
8.4注解
8.4.1 @Component
@Componet: 在类下面增加,使其为组件(一个beans),就能够不同在配置文件中注册。id默认为类名的小写
若是有这样一个类:
public class User { private String name; public int age;}
一般而言,须要在beans.xml 中注入到IoC容器中:<bean id="user" class="com.pojo.User"/>
能够应用上面的注解:
@Component
id : 默认为类名的小写
8.4.2 @Value
@Value:在类中属性下面或者对应的Setter下面,给属性赋值
public class User { @Value("Bob") private String name; @Value("25") public int age;}
Setter上:
@Value("Bob") public void setName(String name) { this.name = name; } @Value("25") public void setAge(int age) { this.age = age; }
等效于:
<bean id="user" class="com.pojo.User"> <property name="name" value="Bob"/> <property name="age" value="25"/> </bean>
简单类型@Value无限,还是得配置文件实现。
8.4.3 @Component 的衍生注解
为了更好的进行分层,Spring能够应用其它三个注解,性能一样,目前应用哪一个性能都一样。
- @Repository ::dao层
- @Service :service层
- @Controller :web层
** @Component 尽管能够间接在类的定义出将其托管给Spring 容器(bean), 但必须在XML配置文件加上反对注解的语句:<context:annotation-config/>
,还无奈齐全脱离配置文件
8.4.4 @Scope
在类下面增加,同 bean标签中的 scope 属性一样,规定作用域的
8.4.5 @Configuration
应用 @Component 时候,必须在XML申明反对注解,而应用@Configuration 就能够脱离配置文件。
定义 User:
@Componentpublic class User { @Value("LiSi") private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}
定义 配置类
@Configurationpublic class Config { @Bean // 办法的名字就相当于 Bean中的id // 返回值相当于 class public User getUser(){ return new User(); }}
这样,就脱离了配置文件。
Test:
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); User user = (User) context.getBean("getUser"); System.out.println(user.getName());
留神:
- 这里应用new AnnotationConfigApplicationContext(Config.class); 去获取配置类
- Bean的id就是@Bean上面函数的名字