b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

Java+Spring+Bean+注入模式

电脑杂谈  发布时间:2019-11-24 01:00:36  来源:网络整理

java bean_java三种bean_bean

1、首先准备共享文件

调用方法Client端Client.java:

java bean_java三种bean_bean

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
    public static void main(String[] args) {
        //创建容器
        ClassPathXmlApplicationContext cac = new ClassPathXmlApplicationContext("service.xml");
        //获取bean对象
        CustomerServiceImpl cs = (CustomerServiceImpl) cac.getBean("CustomerServiceImpl");
        //调用方法
        cs.saveCustomer();
    }
}

接口文件CustomerService.java:

java三种bean_bean_java bean

public interface CustomerService {
    void saveCustomer();
}

2、构造函数方式注入:

java bean_java三种bean_bean

Spring配置文件java三种bean,Service.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="CustomerServiceImpl" class="CustomerServiceImpl">
        <constructor-arg name="name" value="zhangsan"></constructor-arg>
        <constructor-arg name="age" value="3"></constructor-arg>
    </bean>
</beans>

bean_java三种bean_java bean

注入bean类文件:CustomerServiceImpl.java

public class CustomerServiceImpl implements CustomerService {
    private String name ;
    private Integer age;
    public CustomerServiceImpl(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public void saveCustomer() {
        System.out.println("CustomerServiceImpl-saveCustomer-" + name + "-" + age);
    }
}

3、set方式注入

Spring配置文件java三种bean,Service.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="CustomerServiceImpl" class="CustomerServiceImpl">
    <property name="name" value="李四"></property>
    <property name="age" value="10"></property>
</bean>
</beans>

注入bean类文件:CustomerServiceImpl.java:


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-131102-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...