}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List getHobbys() {
return hobbys;
}
public void setHobbys(List hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set getGame() {
return game;
}
public void setGame(Set game) {
this.game = game;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return “Student{” +
“name='” + name + ‘’’ +
“, address=” + address.toString() +
“, books=” + Arrays.toString(books) +
“, hobbys=” + hobbys +
“, card=” + card +
“, game=” + game +
“, wife='” + wife + ‘’’ +
“, info=” + info +
‘}’;
}
}
Address类:
package com.itholmes.pojo;
/*
概述:
@Date:Create in 9:16 2021/12/28
*/
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return “Address{” +
“address='” + address + ‘’’ +
‘}’;
}
}
测试类MyTest:
/*
概述:
@Date:Create in 9:28 2021/12/28
*/
import com.itholmes.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(“beans.xml”);
Student s = (Student) context.getBean(“student”);
System.out.println(s.toString());
//Student{name=‘张三’, address=Address{address=‘上海’}, books=[三国演义, 水浒传, 红楼梦, 西游记], hobbys=[听歌, 玩游戏, 打篮球], card={学号=123, 姓名=itholmes}, game=[123, 456, 789], wife=‘null’, info={password=root, url=jdbc:mysql://localhost:3306/, driver=com.mysql.jdbc.Driver, username=root}}
}
}
不同的类型,通过不同的方式进行注入!
5.4.1 概述
官方文档:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html
p命名和c命名如下:
xmlns:p=“http://www.springframework.org/schema/p”
xmlns:c=“http://www.springframework.org/schema/c”
5.4.2 p-namespace p命名空间
p命名空间注入,可以直接注入属性的值。(这里的p就是property属性的意思
userbeans.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:p=“http://www.springframework.org/schema/p”
xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd”>
User.java类:
package com.itholmes.pojo;
/*
概述:
@Date:Create in 10:11 2021/12/28
*/
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return “User{” +
“name='” + name + ‘’’ +
“, age=” + age +
‘}’;
}
}
MyTest02.java测试类:
/*
概述:
@Date:Create in 10:51 2021/12/28
*/
import com.itholmes.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest02 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(“userbeans.xml”);
User user = context.getBean(“user”,User.class); //在后面添加上了User.class类型后,就不用每次都强转了。
System.out.println(user.toString());
//User{name=‘张三’, age=18}
}
}
这里我们可以通过使用测试类验证,p命名是否注入成功。
5.4.3 c-namespace c命名空间
这里就是传递构造器的内容了,注意如果我们添加了有参构造器,那么无参构造器也要添加,不然前面的默认无参构造器的bean会报错!
c命名空间的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:c=“http://www.springframework.org/schema/c”
xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd”>
User.java类(比起上添加了有参和无参构造器):
package com.itholmes.pojo;
/*
概述:
@Date:Create in 10:11 2021/12/28
*/
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return “User{” +
“name='” + name + ‘’’ +
“, age=” + age +
‘}’;
}
}
同样进行MyTest02.java测试:
/*
概述:
@Date:Create in 10:51 2021/12/28
*/
import com.itholmes.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest02 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(“userbeans.xml”);
User user = context.getBean(“user2”,User.class); //在后面添加上了User.class类型后,就不用每次都强转了。
System.out.println(user.toString());
//User{name=‘itholmes’, age=18}
}
}
c命名空间就是设置有参构造器的,很容易理解。
注意:p命名和c命名空间不能直接使用,需要导入xml约束,所谓的约束就是下面的这段代码:
xmlns:p=“http://www.springframework.org/schema/p”
xmlns:c=“http://www.springframework.org/schema/c”
=================================================================================
在官方上面bean的作用域有六种:
6.2 singleton 单例模式(Spring 默认的模式)
singleton意思是单例。
Spring默认就是单例模式。
通过scope属性设置为singleton:
原型模式和我们认识的多例模式差不多。
scope属性值设置为prototype就可以了。
多线程一般使用原型模式,单线程一般使用单例模式,看情况使用。
MyTest02.java 测试类:
/*
概述:
@Date:Create in 10:51 2021/12/28
*/
import com.itholmes.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest02 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(“userbeans.xml”);
User user = context.getBean(“user2”,User.class); //在后面添加上了User.class类型后,就不用每次都强转了。
User user2 = context.getBean(“user2”,User.class); //在后面添加上了User.class类型后,就不用每次都强转了。
//原型模式就是不同的对象了。
System.out.println(user.hashCode()+“,”+user2.hashCode());
System.out.println(user == user2);
//1709366259,1335298403
//false
}
}
**可以看到上面的结构无论是hashcode还是判断都是不同的对象。
也就是每次从容器中获得时,都会获得一个新对象。**
6.4 request,session ,application
request,session ,application这些个只能在web开发中使用。
就是Servlet的三大域对象一样的。
-
设置为request:对象只能在一次请求内存活。
-
设置为session:对象只能在一次会话内存活。
-
设置为application:对象在项目开始到项目关闭存活。
==========================================================================
自动装配是Spring满足bean依赖的一种方式。
Spring会在上下文中自动寻找,并自动给bean装配属性。
Spring中有三种装配的方式:
-
在xml中显示的配置。(前面)
-
在java中显示配置。
-
隐式的自动装配bean。(重点!)
7.2.1 Autowired 属性
官方给的是:自动装配Autowired。
通过Autowired属性设定。
7.2.2 Autowired的 byName值
byName值:会自动在容器上下文中查找,和自己对象setXxx方法后面的Xxx值对应的beanid
<?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”>
弊端:必须和set后面的名字相同,也就是唯一才能自动装配到。
7.2.3 Autowired的 byType值
byType值:会自动在容器上下文中查找,和自己对象属性类型相同的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”
xsi:schemaLocation=“http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd”>
弊端:type类型必须唯一,不然会直接报错的,同样就算设置的bean没有id,依然能够自动装配,因为是根据类型来的。。
=================================================================================
注解仅仅是JDK1.5以上支持的版本,在Spring中2.5 版本就支持注解了。
使用注解前提:
-
导入约束。
-
配置注解的支持。
官方的注解配置如下:
<?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/
第一步,添加约束:
添加一个约束:
xmlns:context=“http://www.springframework.org/schema/context”
两个约束的支持就是xsi:schemaLocation中的:
**http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd**
第二步,添加注解支持:
context:annotation-config,开启注解的支持。
context:annotation-config\\
**设置好上面的约束后,就可以在对象类中设置@Autowired注解:
(被注解注释的属性必须与xml文件中的bean的id属性相同)**
@Autowired注解注意事项:
-
使用了@Autowired注解后,连set方法都是不需要的!
-
@Autowired注解直接在属性上使用即可!也可以在setXxx方法上使用。
-
使用@Autowired我们可以不用编写setXxx方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在,且bean的id值名字必须相同!
@Nullable注解使用:
@Nullable 某个字段标记了这个注解,说明这个字段可以为null!
@Autowired注解源码如下:
public @interface Autowired {
boolean required() default true;
}
因此,它是有一个required属性的设置,设个属性和@Nullable注解差不多来定义能否为null的。
如果显示定义了Autowired的required属性为false,说明这个对象可以为null;定义为true,就不能为空!
@Autowired注解:
可以自动进行装配匹配xml文件中的bean效果,不用设置property属性了。但是如果有多个bean名字差不多,这是Autowired就会报错了!原因就是不确定找不到是哪一个。如下:
@Qualifier注解:
这个时候就需要@Qualifier(value=“xxx”)来指定xml配置文件中的bean属性了。
代码效果如下:
package com.itholmes.pojo;
/*
概述:
@Date:Create in 14:10 2021/12/28
*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.Nullable;
public class People {
//如果显示定义了Autowired的required属性为false,说明这个对象可以为null;定义为true,就不能为空!。
@Autowired
@Qualifier(value = “cat222”)
private Cat cat;
@Autowired
@Qualifier(value = “dog222”)
private Dog dog;
private String name;
public People() {
}
//@Nullable注解:name为空也不会报错!
public People(String name) {
this.name = name;
}
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return “People{” +
“cat=” + cat +
“, dog=” + dog +
“, name='” + name + ‘’’ +
‘}’;
}
}
<?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
https://www.springframework.org/schema/context/spring-context.xsd">
context:annotation-config/
确定好前后对应即可。
=================================================================================
@Resource注解:
同样,在没有多个bean的相同类型时,@Resource也是可以实现自动装配,name属性也可以指定bean的属性值id。
注意这个Resource注解是javax.annotation.Resource下的,不是spring的东西。
开发中,我们常用的就是@Autowired注解和@Resource注解实现,前者最多。
@Resource和@Autowired的区别:
-
都是用来自动装配的,都可以放在属性字段上。
-
@Autowired通过byType的方式实现,如果设置了@Qualifier(value=“xxx”)则用byname实现,而且必须要求该对象bean存在!
-
@Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现,如果两个都找不到就会报错!。
-
执行顺序不同:@Autowired先是通过byType实现,@Resource先是通过byname实现
=============================================================================
上面说过的注解如下:
@Autowired:spring自动装配,配合@Qualifier(value=“xxx”)来使用。
@Nullable:字段标记了这个注释,说明这个字段可以为null。
@Resource:java自带的自动装配通过名字,类型。
在介绍一个@Component注解:
@Component:Component英文意思就是组件的意思。
这个注解相当于我们自定义的bean标签,例如:
**注意使用@Component注解时,必须指定要扫描的包:
<context:component-scan base-package=“com.itholmes.dao”/>**
换句话说,我们不用自己去xml文件定义对象bean标签了。
他还有一个@Value(“xxx”)注解可以给属性变量添加值,我们之前的@Autowired是给自己创建的引用实例类来自动装配的。
User.java类:
package com.itholmes.dao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//@Component英文意思就是组件的意思。
//等价于
@Component
public class User {
//@Value等价于它:
@Value(“itholmes”)
public String name;
//也可以注入到set方法上面
//@Value(“itholmes”)
public void setName(String name) {
this.name = name;
}
}
beans.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
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:component-scan base-package=“com.itholmes.dao”/>
context:annotation-config/
测试类:
/*
概述:
@Date:Create in 10:13 2021/12/30
*/
import com.itholmes.dao.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);
User user = context.getBean(“user”, User.class);
System.out.println(user.name);
}
}
@Component有几个衍生注解,在web开发中,会按照mvc三层架构分层。
-
dao层 : @Repository
-
service层:@Service
-
controller层:@Controller
换句话说这四个注解的功能是相同的!都是代表某个类注入到Spring容器中,装配Bean。
注意,不要忘记扫描包的的声明!
上面我们通过@Autowired注解来自动装配指定实例类的操作。
我们还可以通过@Value来给类中的属性变量设置值。
@Scope就是对应上面我们设定scope属性的注解,同样值常用的就是singleton单例模式,prototype原型模式。
Scope也就是作用域。
xml 与 注解:
-
xml是万能的,适用于任何场合!维护起来简单方便。
-
注解是定义到类中,因此不是被自己注解的类是不能够使用的。而且维护起来相对复杂一些。
最佳方案如下:
最后
这份清华大牛整理的进大厂必备的redis视频、面试题和技术文档
祝大家早日进入大厂,拿到满意的薪资和职级~~~加油!!
感谢大家的支持!!
cationContext(“applicationContext.xml”);
User user = context.getBean(“user”, User.class);
System.out.println(user.name);
}
}
@Component有几个衍生注解,在web开发中,会按照mvc三层架构分层。
-
dao层 : @Repository
-
service层:@Service
-
controller层:@Controller
换句话说这四个注解的功能是相同的!都是代表某个类注入到Spring容器中,装配Bean。
注意,不要忘记扫描包的的声明!
上面我们通过@Autowired注解来自动装配指定实例类的操作。
我们还可以通过@Value来给类中的属性变量设置值。
@Scope就是对应上面我们设定scope属性的注解,同样值常用的就是singleton单例模式,prototype原型模式。
Scope也就是作用域。
xml 与 注解:
-
xml是万能的,适用于任何场合!维护起来简单方便。
-
注解是定义到类中,因此不是被自己注解的类是不能够使用的。而且维护起来相对复杂一些。
最佳方案如下:
最后
这份清华大牛整理的进大厂必备的redis视频、面试题和技术文档
祝大家早日进入大厂,拿到满意的薪资和职级~~~加油!!
感谢大家的支持!!
[外链图片转存中…(img-nkBhlS9k-1714458663053)]