@Component(“wt_low”)
public class Waiter_low extends Waiter{
public Waiter_low() {
System.out.println(“Waiter_low的无参构造器…”);
}
}
Restaurant.java
package annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
/**
-
演示使用@Autowired注解来
-
完成构造器方式的注入。
-
@author Cher_du
*/
@Component(“rest”)
public class Restaurant {
private Waiter wt2;
public Restaurant() {
System.out.println(“Restaurant的无参构造器…”);
}
@Autowired(required=false)
//@Qualifier指定要注入的bean的id
public Restaurant(@Qualifier(“wt_low”) Waiter wt2) {
System.out.println(“Restaurant的有参构造器…”);
this.wt2 = wt2;
}
@Override
public String toString() {
return “Restaurant [wt=” + wt2 + “]”;
}
}
Restaurant2.java
package annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
/**
-
演示使用@Autowired注解来完成
-
set方式的注入。
-
@author Cher_du
*/
@Component(“rest2”)
public class Restaurant2 {
@Autowired
@Qualifier(“wt”)
private Waiter wt;
public Restaurant2() {
System.out.println(“Restaurant2的无参构造器…”);
}
public Waiter getWt() {
return wt;
}
@Autowired
public void setWt(@Qualifier(“wt”) Waiter wt) {
System.out.println(“Restaurant2的setWt方法…”);
this.wt = wt;
System.out.println(“setWt—over…”);
}
@Override
public String toString() {
return “Restaurant2 [wt=” + wt + “]”;
}
}
Computer.java
package annotation;
import org.springframework.stereotype.Component;
@Component(“cp”)
public class Computer {
public Computer() {
System.out.println(“Computer的无参构造器…”);
}
}
Manager.java
package annotation;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
-
演示使用@Resource注解来完成set方式的注入。
-
只支持set方式注入。
-
@author Cher_du
*/
@Component(“mg”)
public class Manager {
private Computer cp;
@Value(“#{jdbc.pageSize}”)
//使用Value注解来赋值。
//Value注解可以使用spring表达式
private int pageSize;
@Value(“宝玉”)
//Value注解还可以完成基本类型的值的注入
private String name;
public Manager() {
System.out.println(“Manager的无参构造器…”);
}
public Computer getCp() {
return cp;
}
@Resource(name=“cp”)
//name属性指定要注入的bean的id
public void setCp(Computer cp) {
System.out.println(“Manager的setCp方法…”);
this.cp = cp;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return “Manager [cp=” + cp + “, pageSize=” + pageSize + “, name=” + name + “]”;
}
}
app.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”
xmlns:jdbc=“http://www.springframework.org/schema/jdbc”
xmlns:jee=“http://www.springframework.org/schema/jee”
xmlns:tx=“http://www.springframework.org/schema/tx”
xmlns:aop=“http://www.springframework.org/schema/aop”
xmlns:mvc=“http://www.springframework.org/schema/mvc”
xmlns:util=“http://www.springframework.org/schema/util”
xmlns:jpa=“http://www.springframework.org/schema/data/jpa”
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<context:component-scan
base-package=“annotation”/>
<util:properties id=“jdbc”
一线互联网大厂Java核心面试题库
正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!
子
包下面的所有的类,如果该类前面有特定的
注解(比如@Component),则容器会将其纳入
进行管理(相当在配置文件当中,有一个bean)。
–>
<context:component-scan
base-package=“annotation”/>
<util:properties id=“jdbc”
一线互联网大厂Java核心面试题库
[外链图片转存中…(img-RLJDVzQD-1714190623705)]
正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!