主讲singleton(单例模式) 和 prototype(多例模式)的区别
public class Student {
private Integer sid;
private String sname;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Student() {
System.out.println("Student的构造方法");
}
}
public class Test {
public static void main(String[] args) {
/**
* 若spring中有单例模式的bean,在初始化容器时就会创建此对象
* 而多例即原型的bean,会在使用时创建
*/
ApplicationContext ac = new ClassPathXmlApplicationContext("scope.xml");
Student student1 = ac.getBean("student", Student.class);
Student student2 = ac.getBean("student", Student.class);
System.out.println(student1);
System.out.println(student2);
}
}
//当.xml文件中给bean标签设置为单例模式时 构造器只调用了一次
//当.xml文件中给bean标签设置为多例模式时 构造器调用多次
//若设置为单例模式时,系统会默认加载构造器
public class Person {
private Integer id;
private String sex;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
System.out.println("Two:依赖注入");
this.id = id;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person() {
System.out.println("One:创建对象");
}
@Override
public String toString() {
return "Four:使用";
}
public void init(){
System.out.println("Three:初始化");
}
public void destory(){
System.out.println("Five:销毁");
}
}
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("life.xml");
Person person = ac.getBean("person",Person.class);
System.out.println(person);
ac.close();
}
}
//在.xml bean标签中通过设置init-method(初始化方法)和destroy-method(销毁方法)来调用类中自己编写的方法
//销毁方法只有在资源对象关闭之后才会调用
将原本的5步生命周期增加到7步 可以通过后置处理器对方法进行再次编写
//bean的后置处理器 新增的2步
public class AfterHandler implements BeanPostProcessor {
//初始化之前
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
Person person = (Person)bean;
if(person.getSex().equals("男")){
person.setName("张无忌");
}else{
person.setName("赵敏");
}
return person;
}
//初始化之后
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
// TODO Auto-generated method stub
return bean;
}
}
//注:若实现接口 重写方法时 括号内为object o 说明需要导入源码!
//方式一:直接引用
public class Test {
public static void main(String[] args) throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("datasource.xml");
DruidDataSource bean = ac.getBean("datasource1", DruidDataSource.class);
System.out.println(bean.getConnection());
}
}
//方式二:使用jdbc方式 此方法需要事先加载资源文件
db.properties
# k = v
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123456
方式三:直接调用properties
public class Emp {
private Integer eid;
private String ename;
//兼容性问题 子类可以为父类所赋值
private CarExtends car;
//兼容性问题 实现类可以为接口赋值
private DeptI dept;
public Integer getEid() {
return eid;
}
public void setCar(CarExtends car) {
this.car = car;
}
public void setDept(DeptI dept) {
this.dept = dept;
}
public void setEid(Integer eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public CarExtends getCar() {
return car;
}
public DeptI getDept() {
return dept;
}
@Override
public String toString() {
return "Emp [eid=" + eid + ", ename=" + ename + ", car=" + car
+ ", dept=" + dept + "]";
}
}
public class Car extends CarExtends {
private Integer cid;
private String cname;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Car [cid=" + cid + ", cname=" + cname + "]";
}
}
public class Dept implements DeptI{
private Integer did;
private String dname;
public Integer getDid() {
return did;
}
public void setDid(Integer did) {
this.did = did;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Dept [did=" + did + ", dname=" + dname + "]";
}
}
//autowire 自动装配 免去使用ref 可以byName/byType
//byName时 id与class类型必须同名 否则无法转换!
//byType可不同名
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("auto.xml");
Emp emp = ac.getBean("emp", Emp.class);
System.out.println(emp);
}
}
所有注解都是功能相同的 由@Component演变
@Controller //表示此处为控制层
public class UserController {
public UserController(){
System.out.println("UserController");
}
}
@Service //接口无法写入bean中
public class UserServiceImpl implements UserService{
public UserServiceImpl(){
System.out.println("UserServiceImpl");
}
}
@Repository
public class UserDaoImpl implements UserDao {
public UserDaoImpl(){
System.out.println("UserDaoImpl");
}
}
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("user.xml");
UserController uc = ac.getBean("userController",UserController.class);
System.out.println(uc);
UserService us = ac.getBean("userServiceImpl",UserServiceImpl.class);
System.out.println(us);
UserDao ud = ac.getBean("userDaoImpl",UserDaoImpl.class);
System.out.println(ud);
//1.在类上加注解 将需要被spring管理的对象所属的类上加注解
//2.扫描组件 在spring配置文件下加上一个组件扫描 使用包结构
//只有创建容器语句时 也可正确运行 需导入aop.jar!
}
}