Link With DataSource : XML Bean « Spring « Java

Link With DataSource

File: context.xml

<?xml version="1.0" encoding="UTF-8"?>

   <bean id="dataSource"

   <property name="driverClassName">

   <property name="url">

   <property name="username">

   <property name="password">


   <bean id="datacon" class="Dataconimpl">
      <property name="dataSource"><ref local="dataSource"/></property>


import javax.sql.DataSource;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

public class Main {

  public static void main(String[] args) throws Exception {
    BeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml"));
    Dataconimpl bean1 = (Dataconimpl) factory.getBean("datacon");


class Dataconimpl implements Datacon {
  private DataSource dataSource;

  public void setDataSource(DataSource ds) {
    dataSource = ds;

  public DataSource dbcon() {
    return dataSource;

interface Datacon {
  public DataSource dbcon();

       2,893 k)

Related examples in the same category

1.XML Bean Injection
2.Reference another bean and set property
3.Static Factory
4.Serach By Base Package
5.throw RequiredPropertyNotSetException
6.Properties File Based Spring Bean
7.Non Static Factory
8.Local Reference
9.Inheritance Demo
10.HierarchicalBeanFactory Demo
11.Filtered By Annotation
12.destroy method
13.dependency check Demo
14.Custom InitializationMethod
15.component scan
16.Component Scan and scope
17.Component Filter Assignable
18.implements BeanNameAware
19.Bean Lifecycle Initializing
20.Bean Lifecycle DisposableBean
22.Alias Bean Demo